您必须创建一个STA线程来泵送消息循环。对于像WebBrowser这样的ActiveX组件,这是唯一的好客环境。否则,您将不会获得documentCompleted事件。一些示例代码:
private void runBrowserThread(Uri url) { var th = new Thread(() => { var br = new WebBrowser(); br.documentCompleted += browser_documentCompleted; br.Navigate(url); Application.Run(); }); th.SetApartmentState(ApartmentState.STA); th.Start();}void browser_documentCompleted(object sender, WebBrowserdocumentCompletedEventArgs e) { var br = sender as WebBrowser; if (br.Url == e.Url) { Console.WriteLine("Natigated to {0}", e.Url); Application.ExitThread(); // Stops the thread }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)