如何使用BackgroundWorker而不是Application.DoEvent来阻止应用程序挂起

时间:2012-08-24 22:24:01

标签: c# .net backgroundworker doevents

我是c#/ .net的初学者,所以请告诉我如何使用BackgroundWorker而不是Application.DoEvent。

这是我的代码:

while (webBrowser1.ReadyState!= WebBrowserReadyState.Complete)
{
  Application.DoEvents(); //how to use "BackgroundWorker" here 
  if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
  {                            
    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
    listBox1.SelectedIndex = 0;

    var num = listBox1.Items.Count;
    string str = Convert.ToString(num);
    label2.Text = str;                            
  }
}

1 个答案:

答案 0 :(得分:2)

您不需要BackgroundWorker。只需使用DocumentCompleted事件。

 webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;


void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
     //Do your work   
}