我想同步调用webBrowser.Navigate(string urlString),其中webBrowser是windows窗体控件。我是这样做的
...
private delegate void NavigateDelegate(string s);
...
private void Function()
{
NavigateDelegate navigateDelegate =
new NavigateDelegate(this.webBrowser1.Navigate);
IAsyncResult asyncResult =
navigateDelegate.BeginInvoke("http://google.com", null, null);
while (!asyncResult.IsCompleted)
{
Thread.Sleep(10);
}
MessageBox.Show("Operation has completed !");
}
但消息永远不会被推。为什么这段代码不能正常工作?
答案 0 :(得分:5)
不是最好的方法,但你可以使用它......
while (this.webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
Thread.Sleep(100);
}
答案 1 :(得分:4)
而是使用它来检索页面同步:
this.webBrowser.DocumentCompleted += WebBrowserDocumentCompleted;
this.webBrowser.Navigate("http://google.com");
private void WebBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("Operation has completed !");
}