我试图使用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);
}
答案 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";
}