如何在 C# 的控制台应用程序中将计时器与 Web 浏览器一起使用?
How to use timer with web-browser in console application in c#?
我正在尝试一个接一个地加载多个 url,并且我正在使用 timer.tick 函数以便在网页中加载所有 ajax 数据。
- 但问题是控制台应用程序在 timer.tick 调用之前关闭,因为定时器是新线程?
注意****:我还在主要方法中使用 [STAThread],因为如果我不使用它,它会显示有关未加载 .pdb 文件的警告
- 这里[STAThread]的作用是什么?
如何解决这个问题。有什么办法让它等到计时器停止。
下面是代码示例。
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();
}
您可以使用 Console.ReadKey() 或 Console.ReadLine()
停止控制台应用程序退出
你的做法是错误的。您不应该生成一个进程(在本例中为 WebBrowser 控件),然后进入一个紧密循环检查其状态是否发生变化。
相反,开始加载页面,并将代码绑定到 WebBrowser 控件的 DocumentCompleted 事件。这样,当浏览器完成加载页面时,它将执行完成代码(可以加载下一页,或任何你想让它做的事情),而你不必坐在一个循环中监视它,这只是浪费资源。
我正在尝试一个接一个地加载多个 url,并且我正在使用 timer.tick 函数以便在网页中加载所有 ajax 数据。
- 但问题是控制台应用程序在 timer.tick 调用之前关闭,因为定时器是新线程?
注意****:我还在主要方法中使用 [STAThread],因为如果我不使用它,它会显示有关未加载 .pdb 文件的警告
- 这里[STAThread]的作用是什么?
如何解决这个问题。有什么办法让它等到计时器停止。 下面是代码示例。
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();
}
您可以使用 Console.ReadKey() 或 Console.ReadLine() 停止控制台应用程序退出
你的做法是错误的。您不应该生成一个进程(在本例中为 WebBrowser 控件),然后进入一个紧密循环检查其状态是否发生变化。
相反,开始加载页面,并将代码绑定到 WebBrowser 控件的 DocumentCompleted 事件。这样,当浏览器完成加载页面时,它将执行完成代码(可以加载下一页,或任何你想让它做的事情),而你不必坐在一个循环中监视它,这只是浪费资源。