2012-08-24 2 views
0

WebBrowser 개체를 배우기 위해 간단한 자동 서핑 응용 프로그램을 만들고 있습니다.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++;   
    } 

(내 영어 죄송합니다)

답변

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 일 때) sayToRun을 0으로 설정하고 타이머를 중지합니다. – samy

+0

코드에 대한 해결책을 추가했습니다. –

1

이 다음

You could disable your timer in your Time_tick function, 

timer1.Enabled = false; 

의 DoC 완료 이벤트를 다시 활성화 같은 이벤트를 추가 : 여기

TIMER TICK 기능입니다 :

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    if(timesToRun > 0) 
    { 
     timer1.Enabled = true; 
    } 
} 
+0

이 작품 isthis하지만 내가 말에도 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이

관련 문제