等待web浏览器的AJAX内容 [英] Waiting for WebBrowser ajax content

查看:117
本文介绍了等待web浏览器的AJAX内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望暂停我的线程的执行,直到一个特定的div已通过AJAX加载到web浏览器的实例。很显然,我可以连续检查本作的presence div的做这样的事情:

I want to pause the execution of my thread until a particular div has been loaded via ajax into a WebBrowser instance. Obviously I can continuously check for the presence of this div doing something like:

while (Browser.Document.GetElementById("divid") == null) { Thread.Sleep(200); }

不过,睡在线程的浏览器是在循环只有块之间的实际加载在首位的内容浏览器。看来,因此,我需要在一个单独的线程执行 Browser.Navigate 方法 - 那我就继续检查/等待div的presence而在web浏览器实例将继续加载我问它的URL。

However, sleeping the thread that the Browser is in between loops only blocks the browser from actually loading the content in the first place. It seems, therefore, that I need to execute the Browser.Navigate method in a separate thread - I can then continue to check/wait for the presence of the div whilst the WebBrowser instance continues loading the URL I asked it to.

我尝试这一点,但是,都失败了,我会珍惜如何,我应该去了解这个任何输入。我以为只是派遣一个新的线程与新帖(()=> {Browser.Navigate(URL);}); 会工作,但这样做之后的话,没有什么负载和在Browser.ReadyState仍然是未初始化。我presume我误解如何去正确地线程的程序是这样用C#,并会重视一些建议!

My attempts at this, however, have failed and I'd value any input on how I should go about this. I thought simply dispatching a new thread with new Thread(() => { Browser.Navigate(url); }); would work but after doing so, nothing loads and the Browser.ReadyState remains as 'Uninitialized'. I presume I'm misunderstanding how to go about properly threading procedures like this with C# and would value some advice!

推荐答案

不要阻塞主线程的消息泵。由于浏览器是一个STA组件,xmlhtt prequest将无法从后台线程,如果您阻止消息泵引发事件。 您无法浏览在后台线程。 Windows窗体web浏览器的ActiveX不支持从其他线程不是UI线程访问的包装。使用定时器来代替。

Don't block the main thread's message pump. Since the browser is an STA component, xmlhttprequest won't be able to raise events from the background thread if you block the message pump. You can't navigate in a background thread. The Windows Forms wrapper of the webbrowser ActiveX does not support access from other threads than the UI thread. Use a timer instead.

这篇关于等待web浏览器的AJAX内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆