且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在控制台应用程序中使用web浏览器

更新时间:2023-02-13 08:10:03

正确的想法,错执行。该WebBrowser.Navigate()只告诉web浏览器的启动的导航到该网页,你提出的要求。这需要时间,几百毫秒通常的。 Internet Explorer的内部启动线程来完成这项工作。它会告诉你,当它是通过提高DocumentCompleted事件完成。你不等待,这样的暴跌城市第一位。

Right idea, wrong execution. The WebBrowser.Navigate() only tells the web browser to start navigating to the web page you asked for. That takes time, hundreds of milliseconds typically. Internet Explorer internally starts threads to get the job done. It tells you when it is done by raising the DocumentCompleted event. You don't wait for that so that's crash city first.

接下来的问题是,DocumentCompleted事件不会在你的代码得到提升。你必须履行合同STA,它需要你抽消息循环。这就是万能的方式,后台线程,就像IE浏览器用来检索网页的人,讲述的的线程任务完成。

Next problem is that the DocumentCompleted event won't be raised in your code. You have to honor the STA contract, it requires you to pump a message loop. That's the all-mighty way that a background thread, like the one that IE uses to retrieve a web page, tells your thread that the job is done.

您所需要的样板代码是这个答案可用

The boilerplate code you need is available in this answer.