Keep the UI on the main thread, webBrowser_DocumentCompleted, waiting on the same line of code

I want to wait for a while on the webBrowser1_DocumentCompleted event. The reason is that the Webbrowser document stream loads well when the next document finishes. I dont want to block the UI when I wait, and I dont want to trigger Document_Completed_Events.

 private void FormTimer()
    {
        webBrowser1.Navigate("http://www.rolex.com/");
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
    }
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        DownloadCount++;
        alarmCounter = 0;
        Task abc = new Task(() =>
        {
            while (alarmCounter < 5)
            {
                Thread.Yield();
            }
        });
        abc.Start();
        Task.WaitAny(abc);
    }

    void timer_Tick(object sender, EventArgs e)
    {
        alarmCounter++;
    }

      

The problem is that the checkbox stops as it goes to DocumentCompletedEvent.Kindly help me understand the problem and also a good approach for solving these problem situations.

Update: This method works by stripping pending unwanted events and dispatching to DoEvents (). It will also support a UI as well as a timer.

    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        richTextBox1.Text += "\nDocument Started" + documentCount;
        alarmCounter = 0;
        timer.Enabled = true;
        //Do some work
        webBrowser1.DocumentCompleted -= webBrowser1_DocumentCompleted;
        while (alarmCounter < 3)
        {
            Application.DoEvents();            
        }
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;             
        richTextBox1.Text += "\nDocument Completed." + documentCount;
    }

      

+3


source to share


1 answer


Are you using Windows.Forms.Timer? You should be aware that Windows.Forms.Timer runs on the same thread as the UI. So, you call Task.WaitAny, you block the thread, and your timer is stopped.



+1


source







All Articles