C# 等待WebBrowser加载计时器

C# 等待WebBrowser加载计时器,c#,winforms,browser,C#,Winforms,Browser,我正在创建一个简单的自动冲浪应用程序来学习WebBrowser对象 我有一个23个URL的列表,每隔几秒钟我就会浏览一次 应用程序很简单,请转到论坛并打开表单以添加新消息(不发送) 一直走到清单的最后 我的问题是代码forumAction.FillOutFormIn(webBrowser1.Document)在错误的站点中执行 我认为这是因为文件还没有准备好 那么,有没有办法在文档准备好之前停止计时器执行代码呢 以下是定时器滴答声功能: //I start is in 21 for faster

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

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

应用程序很简单,请转到论坛并打开表单以添加新消息(不发送) 一直走到清单的最后

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

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

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

以下是
定时器滴答声
功能:

//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++;          
    }
(对不起我的英语)

添加这样的事件

You could disable your timer in your Time_tick function, 

timer1.Enabled = false;
然后在“单据完成”事件中重新启用:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if(timesToRun > 0) 
    { 
        timer1.Enabled = true;
    }
}
您可以简单地对控件的事件进行编码

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

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();
    }
}

//我在21岁开始做更快的测试。 int timesToRun=21; 私有无效时间(对象发送方、事件参数) {


timmer coad是这项工作,但当它结束时,计时器甚至在我说(当timesToRun为23时)将timesToRun设置为0并停止计时器后重新启动。这项工作但当它结束时,计时器甚至在我说(当timesToRun为23时)将timesToRun设置为0并停止计时器后重新启动
        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++;          
}