2012-08-24 47 views
0

我正在创建一个简单的自动冲浪应用来学习WebBrowser对象。等待WebBrowser加载定时器

我有一个列表,我每冲浪几秒钟就有23个网址。

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

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

(对不起,我的英语)

回答

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

这项工作,但当它结束计时器重新开始,即使我说(当timesToRun是23)设置timesToRun为0,并停止计时器 – samy

+0

我添加了一个解决方案,我的代码。 –

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; 
    } 
} 
+0

这项工作,但是当它到达终点计时器重新开始甚至afther我说(当timesToRun为23)将timesToRun设置为0并停止计时器。 – samy

+0

如果我理解你的话,上面的编辑可能会有所帮助。 –

0

//我开始是在21更快的测试。 int timesToRun = 21; 私人无效Time_Tick(对象发件人,EventArgs的) {

 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++;   
} 

的蒂莫COAD isthis