C#使程序等待浏览器完成加载网页 [英] C# Making program wait until browser finish loading webpage
问题描述
在执行下一条语句之前,如何使程序等待网页加载完成.我尝试了Process :: WaitForInputIdle();但它不会等待.
How do i make my program to wait until webpage finish loading before executing next statement. I tried Process::WaitForInputIdle(); but it doesn't wait.
ProcessStartInfo pInfo = new ProcessStartInfo("firefox.exe");
pInfo.Arguments = "http://example.com";
Process p = Process.Start(pInfo);
p.WaitForInputIdle();
pInfo.Arguments = "http://example.net";
p = Process.Start(pInfo);
p.WaitForInputIdle();
推荐答案
您不能.这里的问题是网页加载后,Firefox无法通讯回来.例如,当Firefox要在打开网页之前进行自我更新时,您将看到问题.页面加载后如何通讯回来?在这之间,整个firefox.exe可执行文件已被替换,与您的 Process
的连接早已丢失.
You can't. The problem here is that Firefox does not communicate back when the web page has loaded. You will for example see issues when Firefox wants to update itself before it opens the web page. How is it going to communicate back when the page is loaded? In between, the entire firefox.exe executable has been replaced, and the connection with your Process
has been long lost.
WaitForInputIdle
做的非常具体,这不是您期望的.Windows通过消息泵工作.当你将鼠标移到窗口上,会向该窗口发送一条消息. WaitForIdleInput
在应用程序处理了它收到的第一条消息时返回,因此,当Windows知道它是响应性的"时.
The WaitForInputIdle
does a very specific job, and this is not what you expect. Windows works through a message pump. When you e.g. move the mouse over a window, a message is send to that window. WaitForIdleInput
returns when the application has processed the first message it has received, so when Windows knows it is 'responsive'.
这篇关于C#使程序等待浏览器完成加载网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!