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.
Note****: I am also using [STAThread] for main method because if I dont use it it showing an warning regarding .pdb file not loaded
How to solve this problem. Is there any way to make it wait till timer is stopped. Here is code samples below.
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++;
}
}
static void Main(string[] args)
{
OpenURLInBrowser("https://www.google.co.in/");
Console.ReadKey();
}
You can use Console.ReadKey() or Console.ReadLine() to stop the console application exit
You're going about it the wrong way. You shouldn't spawn a process (in this case the WebBrowser control) and then go into a tight loop checking for its status to change.
Instead, start the page loading, and bind code to the WebBrowser control's DocumentCompleted event. That way, when the browser has completed loading the page, it will execute the completion code (which could load the next page, or whatever you want it to do), without you having to sit and monitor it in a loop, which just wastes resources.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.