我正在尝试在Windows窗体应用程序中使用进度条,该应用程序将在成功完成后验证登录凭据,它显示另一种窗体,但由于必须显示而无法显示任何内容,因此无法显示它。瞬间让我回到了原始形式 取消按钮也不起作用 请任何人都可以给我解决这个问题的方法
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if(textBox1.Text == "Abrar" && textBox2.Text=="abrar")
{
//progressBar1.Visible = true;
backgroundWorker1.ReportProgress(i);
Thread.Sleep(3000);
backgroundWorker1.ReportProgress(i * 2);
Thread.Sleep(3000);
backgroundWorker1.ReportProgress(i * 3);
Thread.Sleep(3000);
backgroundWorker1.ReportProgress(i * 4);
Form2 f = new Form2();
f.Show();
Form1 d1 = new Form1();
d1.Hide();
}
if(backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Cancelled)`enter code here`
{
label3.Text = "Cancelled";
}
else
{
label3.Text = "Login Successful";
}
}
private void Login_Click(object sender, EventArgs e)
{
if(!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
else if(backgroundWorker1.IsBusy)
{
label3.Text = "Process is running";
}
}
private void Form1_Load(object sender, EventArgs e)
{
// progressBar1.Visible = false;
}
private void Cancel_Click(object sender, EventArgs e)
{
if(backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
}
}
答案 0 :(得分:0)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if(textBox1.Text == "Abrar" && textBox2.Text=="abrar")
{
//progressBar1.Visible = true;
backgroundWorker1.ReportProgress(i);
Thread.Sleep(3000);
backgroundWorker1.ReportProgress(i * 2);
Thread.Sleep(3000);
backgroundWorker1.ReportProgress(i * 3);
Thread.Sleep(3000);
backgroundWorker1.ReportProgress(i * 4);
}
if(backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Cancelled)`enter code here`
{
label3.Text = "Cancelled";
}
else
{
label3.Text = "Login Successful";
Form2 f = new Form2();
f.Show();
Form1 d1 = new Form1();
d1.Hide();
this,Hide();
}
}
private void Login_Click(object sender, EventArgs e)
{
if(!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
else if(backgroundWorker1.IsBusy)
{
label3.Text = "Process is running";
}
}
private void Form1_Load(object sender, EventArgs e)
{
// progressBar1.Visible = false;
}
private void Cancel_Click(object sender, EventArgs e)
{
if(backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
}
}