Multi Threading in WinForms
-
Greetings, I'm in .NET 2.0 I'm working on an app where the Main Form contains a webBrowser Control. I do not want to show the Main Form until the document has completed loading in the web browser control or until 60 seconds has passed...in which case, I will then load an offline version. The webBrowser control has a DocumentCompleted event. So that I know if it has finished loading. The problem is, I need to Load the url for the webBrowser control on a separate thread, and then loop for 60 seconds on the GUI Thread, or Until Ive been notified that the document has loaded. I have tried something like this....but I think the problem is that the webBrowser Control belongs to the GUI Thread.
//Constructor
public frmMain()
{
ThreadStart ts = new ThreadStart(DoWork);
Thread thread = new Thread(ts);
thread.Start();//Give one minute to find the site
for (int i = 0;i<60;i++)
{ if (m_documentLoaded)
{
i = 60;
}
else
{
System.Threading.Thread.Sleep(1000);
}
}
}public void DoWork()
{
//Fill the URL
System.Uri url = new System.Uri("http://myDomain.com/default.aspx/");
webBrowser1.Url = url;
}//Event for webBrowser Control...fires when document completes loading
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//now show the form if it is hidden.
m_documentLoaded = true;}
I know what I want to Do...just not sure exactly how to carry it out... Thanks for any input
-
Greetings, I'm in .NET 2.0 I'm working on an app where the Main Form contains a webBrowser Control. I do not want to show the Main Form until the document has completed loading in the web browser control or until 60 seconds has passed...in which case, I will then load an offline version. The webBrowser control has a DocumentCompleted event. So that I know if it has finished loading. The problem is, I need to Load the url for the webBrowser control on a separate thread, and then loop for 60 seconds on the GUI Thread, or Until Ive been notified that the document has loaded. I have tried something like this....but I think the problem is that the webBrowser Control belongs to the GUI Thread.
//Constructor
public frmMain()
{
ThreadStart ts = new ThreadStart(DoWork);
Thread thread = new Thread(ts);
thread.Start();//Give one minute to find the site
for (int i = 0;i<60;i++)
{ if (m_documentLoaded)
{
i = 60;
}
else
{
System.Threading.Thread.Sleep(1000);
}
}
}public void DoWork()
{
//Fill the URL
System.Uri url = new System.Uri("http://myDomain.com/default.aspx/");
webBrowser1.Url = url;
}//Event for webBrowser Control...fires when document completes loading
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//now show the form if it is hidden.
m_documentLoaded = true;}
I know what I want to Do...just not sure exactly how to carry it out... Thanks for any input
Create a timer on your form with interval set to 60 seconds. Load the browser page on the UI thread, start the timer. When the timer tick event has been raised, check if m_documentLoaded is true, and if not, load the offline version.
-
Create a timer on your form with interval set to 60 seconds. Load the browser page on the UI thread, start the timer. When the timer tick event has been raised, check if m_documentLoaded is true, and if not, load the offline version.