Davood
Davood

Reputation: 5645

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"); 
}

Upvotes: 0

Views: 2178

Answers (2)

Darey
Darey

Reputation: 497

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

Upvotes: 2

Erx_VB.NExT.Coder
Erx_VB.NExT.Coder

Reputation: 4856

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.

Upvotes: 0

Related Questions