Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to load multiple url's one after another and I am using timer.tick function so that all ajax data loads in the webpage.

1. But problem is that console application closes before timer.tick is call after interval of timer because timer is of new thread?

Note****: I am also using [STAThread] for main method because if I dont use it it showing an warning regarding .pdb file not loaded

2. What is the role of [STAThread] here?

How to solve this problem. Is there any way to make it wait till timer is stopped. Here is code samples below.

C#
static void Main(string[] args)
    {
        OpenURLInBrowser("https://www.google.co.in/");
    }
private static void OpenURLInBrowser(String url)
    {
        if (!url.StartsWith("http://") && !url.StartsWith("https://"))
        {
            url = "http://" + url;
        }
        try
        {
            webbrowser.AllowNavigation = true;
            webbrowser.ScriptErrorsSuppressed = true;
            webbrowser.Navigate(new Uri(url));
            WaitTillPageLoadsCompletly(DynamicWebBrowser.webbrowser);
            timer.Interval = 10000;
            timer.Tick += Timer_Tick;
            timer.Start();
        }
        catch (UriFormatException)
        {
            return;
        }
    }
    private static void Timer_Tick(object sender, EventArgs e)
    {
        if (webbrowser.ReadyState == WebBrowserReadyState.Complete)
        {
            HtmlElement element = webbrowser.Document.GetElementById("loadingDiv");
            if (element != null)
            {
                Console.Write(element.OuterHtml + "\n\n\n");
                Console.WriteLine("==============================================================================================================" + "\n\n\n");
                timer.Stop();
                int count = 0;
                while (count < 2)
                {
                    OpenURLInBrowser("https://www.google.co.in/");
                    count++;
                }
            }
        }
    }
private static void WaitTillPageLoadsCompletly(WebBrowser webBrControl)
    {
        WebBrowserReadyState loadStatus;
        int waittime = 20000;
        int counter = 0;
        while (true)
        {
            loadStatus = webBrControl.ReadyState;
            Application.DoEvents();
            if ((counter > waittime) || (loadStatus == WebBrowserReadyState.Uninitialized) || (loadStatus == WebBrowserReadyState.Loading) || (loadStatus == WebBrowserReadyState.Interactive))
            {
                break;
            }
            counter++;
        }
        counter = 0;
        while (true)
        {
            loadStatus = webBrControl.ReadyState;
            Application.DoEvents();
            if (loadStatus == WebBrowserReadyState.Complete && webBrControl.IsBusy != true)
            {
                break;
            }
            counter++;
        }
    }


What I have tried:

The above code is what I tried.
Posted
Updated 7-Oct-16 14:17pm
Comments
[no name] 7-Oct-16 9:16am    
1. Its exiting because your main method has finished running. When main exits, so does your application.
2. There is no [STAThread] in your code, but if there was, that makes your main thread a single thread apartment. A simple google search would have told you that.
Nithin B 12-Oct-16 6:27am    
Yes I do have [STAThread] of main method but I accidentally did not put in the code above. I want to know what is the role of it here?

Yes, your code don't have STA thread. if you putting timer to get time for your html loading, that is not required when you use document completed event

Please try this

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {

        public static void Main(string[] args)
        {
            string url = "https://www.google.co.in/";
            Thread thread = new Thread(delegate ()
             {
                 using (WebBrowser browser = new WebBrowser())
                 {
                     browser.ScrollBarsEnabled = false;
                     browser.AllowNavigation = true;
                     browser.Visible = true;
                     browser.Navigate(url);
                     browser.Width = 1024;
                     browser.Height = 768;
                     browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted);


                     while (browser.ReadyState != WebBrowserReadyState.Complete)
                     {
                         System.Windows.Forms.Application.DoEvents();
                     }
                 }
             });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            Console.ReadKey();
        }


        public static void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrControl = sender as WebBrowser;
            if (webBrControl.ReadyState == WebBrowserReadyState.Complete)
            {
                HtmlElement element = webBrControl.Document.GetElementById("prm");
                if (element != null)
                {
                    Console.Write(element.OuterHtml + "\n\n\n");
                    Console.WriteLine("==============================================================================================================" + "\n\n\n");
                }
            }
        }
    }
}
 
Share this answer
 
Not only do you not have an STA thread, you also do not have a message pump, without which the WebBrowser control will not work correctly.

See this[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900