简体   繁体   中英

Web browser control hangs my application

I have a button and a web browser control on my WinForms application. 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

After the .navigate method, do this...

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.

Ket me know how it works out for you and if I can be of any further help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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