Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multi Threading in WinForms

Multi Threading in WinForms

Scheduled Pinned Locked Moved C#
csharpwinformscomhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shawn_H
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • S Shawn_H

      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

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        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.

        S Offline
        S Offline
        Shawn_H
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups