可以使用backgroundworker作为等待吗?

时间:2018-03-02 17:57:28

标签: c#

我试图使用backgroundworker作为等待。目标是使程序等待1000ms backgroundworker

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = "Waiting";
    backgroundWorker1.RunWorkerAsync();
    label1.Text = "Done";
}

private async void backgroundWorker1_DoWork_1(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    await Task.Delay(1000);
}

1 个答案:

答案 0 :(得分:-1)

像其他人在评论中建议的那样使用RunWorkerCompleted方法。

private void button1_Click(object sender, EventArgs e)
{
  label1.Text = "Waiting";
  backgroundWorker1.RunWorkerCompleted += RunWorkerCompleted;
  backgroundWorker1.RunWorkerAsync();
}

private async void backgroundWorker1_DoWork_1(object sender, System.ComponentModel.DoWorkEventArgs e)
{
  await Task.Delay(1000);
}

private async void RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
  label1.Text = "Done";
}