2011-10-26 2 views
0

저는 C# 프로그래밍 언어의 초보자입니다. 간단한 웹 브라우저를 창 형태로 배치했습니다. 나는 브라우저에 url 주소를 할당하고 나는 브라우저가 내가 제공 한 링크를 성공적으로 열 었는지보고 싶다.DocumentCompleted

나는

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 

그러나, 브라우저의 URL을 할당 한 후, 나는

if (webBrowser1_DocumentCompleted) 
    { 
    //my code here 
    } 

이 가능하다 같은 것을 쓰고 싶은라는 이벤트 핸들러가 있는지 알아? "WebBrowserReadyState"를 사용할 수는 있지만 문서 준비를 시도하고 사용하는 것이 좋습니다.

+0

는 확실하지 나는 문은 기본적으로 경우 이벤트가 무엇을하고 있는지 정확히 따릅니다. 이 이벤트는 webBrowser1_DocumentCompleted가 발생하면 트리거됩니다. 그런 식으로 if 문 내에서 이벤트를 사용할 수 없습니다. – MaxSan

+0

그럴 수 없다. 브라우저가 페이지를 다운로드하는 데 시간이 걸린다. DocumentCompleted 이벤트가 발생할 때까지는 아무 것도 할 수 없습니다. 기다리려고하면 프로그램이 교착 상태가됩니다. –

답변

3

잘 모르겠어요를

을 먼저 폼 클래스의 생성자에 이벤트 처리기를 만들 :

public void Form1() 
{ 
    webBrowser1.DocumentCompleted += 
    new WebBrowserDocumentCompletedEventHandler(WebDocumentCompleted); 
} 
이 당신이 찾고있는하지만 내가 시도 할 것입니다 어떤 경우 이 후 16,

는 해당 이벤트가 발생 될 때 호출되는 메소드를 작성해야합니다 :이 도움이

void WebDocumentcompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    //Your code here 
} 

희망을!

1

웹 페이지로드 및 렌더링이 비동기 적으로 실행되기 때문에 이벤트 메서드에서 논리 (문서가로드 된 후 실행해야 함)를 수행해야합니다. 이 방법으로 이벤트를 구독 할 수 있습니다 :

webBrowser.DocumentCompleted += webBrowser_DocumentCompleted; 

을 당신은 당신이 원하는 코딩을 할 수있는이 서명하여 클래스의 메소드가 있어야 :

void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    // Do something after the document is loaded. 
} 
0

당신은 DownloadDataCompletedEventArgs (예)의 결과를 검사 할 수 있습니다

class Program 
    { 
     static void Main(string[] args) 
     { 

      WebClient wb = new WebClient(); 
      wb.DownloadDataAsync("www.hotmail.com"); 
      wb.DownloadDataCompleted += new DownloadDataCompletedEventHandler(wb_DownloadDataCompleted); 
     } 

     static void wb_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) 
     { 
      if (e.Cancelled)//cancelled download by someone/may be you 
      { 
       //add necessary logic here 
      } 
      else if (e.Error)// all exception can be collected here including invalid download uri 
      { 
       //add necessary logic here 
      } 
      else if (e.UserState)// get user state for asyn 
      { 
       //add necessary logic here 
      } 
      else 
      { 
       //you can assume here that you have result from the download. 
      } 

     } 
    } 
관련 문제