简体   繁体   English

Web浏览器控件使我的应用程序挂起

[英]Web browser control hangs my application

I have a button and a web browser control on my WinForms application. 我的WinForms应用程序上有一个按钮和一个Web浏览器控件。 When I click on a button it fires up a browser, but until page is loaded my application hangs even though I use a separate thread for browser control. 当我单击一个按钮时,它将启动浏览器,但是直到页面加载完毕,即使我使用单独的线程进行浏览器控制,我的应用程序仍挂起。

How can I handle it so my application doesn't hang while page gets loaded? 如何处理它,以使我的应用程序在页面加载时不会挂起?

private void button1_Click(object sender, EventArgs e)
{
    Thread mythread = new Thread(new ThreadStart(go));    
    mythread.IsBackground = true;
    mythread.Name = "mythread";
    mythread.Start();
}

void go() 
{ 
    webBrowser1.Navigate("google.com"); 
}

I had a similar issue, the issue was resolved by using dispatcher. 我有一个类似的问题,该问题已通过使用调度程序解决。 Using Background worker would throw you an error since you are trying in code behind. 使用后台工作程序会引发错误,因为您正在尝试在后面的代码中尝试。

Try the below in button click. 请尝试以下按钮单击。

Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => { go(); })); 

Namespace for the above is System.Threading 上面的命名空间是System.Threading

After the .navigate method, do this... 在.navigate方法之后,执行此操作...

Do
Sleep 100
Doevents
Loop until webbrowser1.busy = true and webbrowser1.readystate = 4

This should sort at thread out, .busy could be .isbusy on your version of wb control, look out my other web browser related answers if you want to implement a proper, more detailed method on pausing execution until web browser is truly done loading a webpage. 这应该在线程输出上进行排序,在您的wb控件版本上,.busy可能是.isbusy,如果您想在暂停执行直到Web浏览器真正完成加载之前实现适当,更详细的方法,请查看其他与Web浏览器相关的答案。网页。

Ket me know how it works out for you and if I can be of any further help. 让我知道这对您有什么帮助,如果我能为您提供进一步的帮助。

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

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