【问题标题】:How to use timer with web-browser in console application in c#?如何在 C# 的控制台应用程序中使用带有 Web 浏览器的计时器?
【发布时间】:2017-02-16 11:33:36
【问题描述】:

我正在尝试一个接一个地加载多个 url,并且我正在使用 timer.tick 函数,以便将所有 ajax 数据加载到网页中。

  1. 但问题是控制台应用程序在 timer.tick 之前关闭,因为 timer 是新线程,所以在计时器间隔之后调用?

注意****:我也在使用 [STAThread] 作为 main 方法,因为如果我不使用它,它会显示有关 .pdb 文件未加载的警告

  1. [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++;
        }
    }

【问题讨论】:

  • 您可以使用 Console.ReadKey() 来阻止控制台关闭

标签: c# c#-4.0 timer webbrowser-control windows-applications


【解决方案1】:
static void Main(string[] args)
    {
        OpenURLInBrowser("https://www.google.co.in/");
        Console.ReadKey();
    }

您可以使用 Console.ReadKey() 或 Console.ReadLine() 停止控制台应用程序退出

【讨论】:

  • 是的,它会停止控制台退出,但不会等到 Web 浏览器完全加载并调用 timer.tick 函数
【解决方案2】:

你走错路了。您不应该生成一个进程(在本例中为 WebBrowser 控件),然后进入一个紧密循环检查其状态是否发生变化。

相反,开始加载页面,并将代码绑定到 WebBrowser 控件的 DocumentCompleted 事件。这样,当浏览器完成页面加载后,它将执行完成代码(可以加载下一页,或任何您希望它执行的操作),而无需您坐下来循环监视它,这只是浪费资源。

见:https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted(v=vs.110).aspx

【讨论】:

  • 我正在尝试在控制台应用程序中执行此操作。只有函数没有页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 2011-02-12
  • 1970-01-01
  • 2012-06-03
  • 2013-07-15
  • 2012-04-28
相关资源
最近更新 更多