0

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

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

该应用程序很简单,转到论坛并打开表格以添加新消息(不发送)并继续前进,直到您到达列表末尾。

我的问题是代码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++;          
    }

(对不起我的英语不好)

4

3 回答 3

1

添加这样的事件

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;
    }
}
于 2012-08-24T13:20:00.177 回答
1

您可以简单地编写控件的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();
    }
}
于 2012-08-24T13:20:48.570 回答
0

//我开始是在 21 进行更快的测试。int timesToRun = 21; 私有 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++;          
}

timmer coad isthis

于 2014-12-21T05:13:44.650 回答