代码之家  ›  专栏  ›  技术社区  ›  samy

正在等待在计时器中加载WebBrowser

  •  0
  • samy  · 技术社区  · 12 年前

    我正在创建一个简单的自动冲浪应用程序来学习WebBrowser对象。

    我每隔几秒钟就会浏览一次23个URL的列表。

    该应用程序很简单,进入论坛并打开FORM添加新消息(无需发送) 一直走到最后。

    我的问题是代码 forumAction.FillOutFormIn(webBrowser1.Document); 在错误的站点执行。

    我认为这是因为文件还没有准备好。

    那么,有没有办法在文档准备好之前停止计时器执行代码?

    这是 TIMER TICK 功能:

    //I start is in 21 for faster testing.
    int timesToRun = 21;
        private void Time_Tick(object sender, EventArgs e)
        {
    
                Console.WriteLine(timesToRun.ToString());
                string currentSite = siteList.GetSiteFromIndex(timesToRun);
    
                webBrowser1.Document.Window.Navigate(currentSite);
    
                //I think I need to wait here until the document is ready
    
                //this line of code doesn't run on timeToRun = 22
                forumAction.FillOutFormIn(webBrowser1.Document);
    
                Console.WriteLine(webBrowser1.Url);
                timerLabel1.Text = siteList.SiteLists.Count + ">" + timesToRun + "";
    
                if (timesToRun >= siteList.SiteLists.Count - 1)
                {
                    enableControls(true);
                    timesToRun = 0;
                    timer.Stop();
                    Console.WriteLine("done");
    
                }
    
                timesToRun++;          
        }
    

    (对不起我的英语)

    3 回复  |  直到 12 年前
        1
  •  1
  •   Emond    12 年前

    您可以简单地对 DocumentCompleted 控制事件。

    这将允许您在加载页面时重新启动计时器。

    webBrowser1.Navigated += WebBrowser_DocumentCompleted;
    timesToRun = 22;
    
    private void Time_Tick(object sender, EventArgs e)
    {
        timer.stop();
        webBrowser1.Document.Window.Navigate(url);
    }
    
    void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        timesToRun--;
        if(timesToRun > 0)
        {
            timer.Start();
        }
    }
    
        2
  •  1
  •   Justin Harvey    12 年前

    添加这样的事件

    You could disable your timer in your Time_tick function, 
    
    timer1.Enabled = false;
    

    然后在doc completed事件中重新启用:

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if(timesToRun > 0) 
        { 
            timer1.Enabled = true;
        }
    }
    
        3
  •  0
  •   microsoft vusial stuio    10 年前

    //我开始是在21更快的测试。 int timesToRun=21; private void Time_Tick(对象发送方,EventArgs e) {

            Console.WriteLine(timesToRun.ToString());
            string currentSite = siteList.GetSiteFromIndex(timesToRun);
    
            webBrowser1.Document.Window.Navigate(currentSite);
    
            //I think I need to wait here until the document is ready
    
            //this line of code doesn't run on timeToRun = 22
            forumAction.FillOutFormIn(webBrowser1.Document);
    
            Console.WriteLine(webBrowser1.Url);
            timerLabel1.Text = siteList.SiteLists.Count + ">" + timesToRun + "";
    
            if (timesToRun >= siteList.SiteLists.Count - 1)
            {
                enableControls(true);
                timesToRun = 0;
                timer.Stop();
                Console.WriteLine("done");
    
            }
    
            timesToRun++;          
    }
    

    蒂默·科德