Multi Threading in WinForms

Posted on 2006-05-02
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 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("");  
  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 85

    Accepted Solution

    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....
    LVL 2

    Author Comment

    Excellent.... once again...I was overthinking.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    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.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now