简体   繁体   English

如何释放“繁忙”的C#Web浏览器

[英]How to free “busy” c# webbrowser

I am using c# and webbrowser to scrape a web page. 我正在使用c#和webbrowser刮取网页。

Sometimes, I will run into a "Message from webpage" popup error. 有时,我会遇到“来自网页的消息”弹出错误。

I use: 我用:

hwnd = FindWindow("#32770", "Message from webpage");
hwnd = FindWindowEx(hwnd, IntPtr.Zero, "Button", "OK");
SendMessage(hwnd, 0xf5, IntPtr.Zero, IntPtr.Zero);

To press ok. 按确定。 However when I try to navigate again, I'll get an error that the webbrowser is still busy. 但是,当我尝试再次导航时,会收到一个错误,表明Web浏览器仍很忙。 How can I force it to become free again? 我该如何强迫它再次变得免费?

The only workaround I have found is to try to "restart" the browser. 我发现的唯一解决方法是尝试“重新启动”浏览器。 I am doing the following: 我正在执行以下操作:

ie.Dispose();
ie = new System.Windows.Forms.WebBrowser();

This seems to work, but as soon as I navigate, I get a "A script on this page is causing Internet Explorer to run slowly" popup. 这似乎可行,但是一旦导航,就会弹出“此页面上的脚本导致Internet Explorer运行缓慢”的弹出窗口。 I can still continue navigating but I notice that overtime I have a rather large memory leak. 我仍然可以继续浏览,但是我发现加班时间我有相当大的内存泄漏。 I'm not sure if it is from the way I have "restarting" the browser or from leaving the popup up. 我不确定是从“重新启动”浏览器还是从弹出窗口中退出。

If you have a way to "free" the browser or if you can confirm the correct way to restart a browser, that would be greatly appreciated. 如果您有“释放”浏览器的方法,或者可以确认重新启动浏览器的正确方法,将不胜感激。

It certainly seems that you are using Dispose() correctly to destroy (elegantly) the WebBrowser class. 当然,您似乎正确地使用了Dispose()来破坏(优雅地)WebBrowser类。 And creating it with new is fine also. 并且用new创建它也很好。 (Just take care of which thread you use - as per link below) (只需注意您使用的是哪个线程-如下面的链接所示)

According to here , you could try a Stop() and Refresh() sequence, although this may not prevent the memory leak. 根据这里 ,您可以尝试使用Stop()Refresh()序列,尽管这可能无法防止内存泄漏。

It's hard to tell from your scant code if the leak is due to the Browser class, your code or the webpages themselves...(Most likely the web pages, considering the amount of buggy webpages) 很难通过您的代码不足来判断泄漏是由于Browser类,您的代码还是网页本身引起的(考虑到存在漏洞的网页数量,很有可能是网页)

But if speed is not important, disposing and renewing the WebBrowser class should not be an issue 但是,如果速度并不重要,则部署和更新WebBrowser类应该不是问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM