2

I have a method browseWebsite() which browses a website using a webbrowser control to change data within that website automatically. The webbrowser control gets instantiated when the method gets executed. Unfortunately parts of the website that gets opened is loaded through ajax so the webbrowser control cannot detect the correct time of loading completion of the website, which means I cannot use the DocumentCompleted event to move further when the page is loaded. To work around that I instantiate a System.Windows.Forms.Timer to wait for 10 seconds until he makes the browser to do the move further on the website. (Thread sleep does not work since the webbrowser freezes together with the thread and stops the loading process) The whole thing looks like this:

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(() => browseWebsite());
            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();

        }

        public void browseWebsite()
        {
            WebBrowser browser = new WebBrowser();

            browser.Navigate("somesite");
        Timer waitTimer = new Timer();
        waitTimer.Interval = 10000;
        waitTimer.Tick +=  delegate (object sender, EventArgs e) { WaitTimer_Tick(sender, e, browser); };

        waitTimer.Start();
    }

    private void WaitTimer_Tick(object sender, EventArgs e, WebBrowser browser)
    {
        browser.Navigate("somewhere else");
    }

Since the complete process this method does is pretty long and complex and I don't want to mess up my main thread having 50 of those webrowsers clicking around within it, I'd like to start that procedure within another Thread. Unfortunately the thread dies when the last line of the method is reached and takes the webbrowser and the timer with it. Is there a way to instantiate a procedure like that? Or keep the thread alive?

4
  • There is no thread!? Use another timer: System.Timers.Timer or better System.Threading.Timer. Commented Apr 28, 2016 at 7:58
  • my bad, changed the Form1_Load event. Commented Apr 28, 2016 at 8:01
  • The thread doesn't "die" - it is finished. Or do you get an exception? You need to wait inside the thread - look at AutoResetEvent or ManualResetEvent. And use another timer. Commented Apr 28, 2016 at 8:03
  • no exception, it just finishes. I had a look into it, but Ivan Stoev's solution works perfectly. But thanks anyway :) Commented Apr 28, 2016 at 8:55

1 Answer 1

1

You can use the parameterless Application.Run method overload

Begins running a standard application message loop on the current thread, without a form.

public void browseWebsite()
{
    // ...
    waitTimer.Start();
    Application.Run();
}

But note that at some later point (when done processing), you'll need to call Application.ExitThread method (make sure you call it from the same thread)

Exits the message loop on the current thread and closes all windows on the thread.

Finally, not directly related, but when using such threads, it's good to set Thread.IsBackground to true in order to not prevent your app process from terminating when the primary thread (main application thread) terminates.

Sign up to request clarification or add additional context in comments.

3 Comments

Works perfectly! Thank you very much! And thanks for pointing out to Thread.IsBackground. That would have become a problem at some point!
While it works, I don't think it's a good solution. There's no reason for another message loop other than using the wrong timer class. And it's not too obvious how to call Application.ExitThread from the right thread.
@JeffRSon It's not another message loop. OP creates a new STA thread, allocates UI components on it (WebBrowser, Timer) so it really needs a message loop. Another (nested) message loop is when you call Application.DoEvents for instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.