[Webinar] Learn how to a build a cloud-first strategyRegister Now


Multi Threading in WinForms

Posted on 2006-05-02
Medium Priority
Last Modified: 2012-06-21

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.

public frmMain()
  ThreadStart ts = new ThreadStart(DoWork);  
  Thread thread = new Thread(ts);  

  //Give one minute to find the site    
  for (int i = 0;i<60;i++)  
   if (m_documentLoaded)    
     {       i = 60;     }    
    { 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

Question by:Shawn_H
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16592780
Why do you need a seperate thread for this?

Simply try adding a Timer control to your form and setting its Inverval to one minute (60,000 milliseconds).  When you navigate, enable the Timer.  If the Timer event fires before the DocumentComplete() event then you have a TimeOut.  If the opposite occurs then disable the Timer....

Author Comment

ID: 16595691
Excellent.... once again...I was overthinking.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question