2013-08-23 2 views
0

웹 사이트에서 가입 옵션을 찾을 수있는 간단한 도구를 만들었습니다 (200 개의 웹 사이트 목록은 arraylist에 있습니다). webbrowser를 사용하고 있었지만 캐시와 쿠키 문제가있어서 webclient로 전환했습니다. 중단 점을 넣고 디버깅 할 때 정상적으로 작동하지만 정상적으로 실행하면 가입 옵션이없는 웹 사이트도 포함됩니다. 다음은 내 코드입니다C# Webclient가 제대로 작동하지 않습니다.

private void btnSearch_Click(object sender, EventArgs e) 
    {   
      timer1.Enabled = true; 
      timer1.Start(); 
    } 

타이머 1 코드

string st; 
     private void timer1_Tick(object sender, EventArgs e) 
     { 
       st = ""; 
Application.DoEvents();     
         try 
         { 
          st = lst[dataindex2].ToString();  
          using (WebClient asyncWebRequest = new WebClient()) 
          { 
           asyncWebRequest.DownloadDataCompleted += asyncWebRequest_DownloadDataCompleted; 
           Uri urlToRequest = new Uri(st); 
           asyncWebRequest.DownloadDataAsync(urlToRequest); 
           asyncWebRequest.Dispose(); 
          } 

          dataindex2++; 
          if (dataindex2 == lst.Count) 
          { 
           timer1.Stop();         
           lblStatus.Text = "Stopped"; 
           lblStatus.ForeColor = Color.DarkRed; 
           MessageBox.Show("Search Completed");         
          } 
         } 
         catch (Exception ex) 
         { 
          timer1.Stop();        
          lblStatus.Text = "Stopped"; 
          lblStatus.ForeColor = Color.DarkRed; 
          timer1.Dispose(); 
          MessageBox.Show(ex.Message); 
          return; 
         } 

asyncWebRequest_DownloadDataCompleted 코드 :

private void asyncWebRequest_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) 
    { 
     if (e.Error != null) 
     { 
      timer1.Stop(); 
      ena(); 
      lblStatus.Text = "Stopped"; 
      lblStatus.ForeColor = Color.DarkRed; 
      timer1.Dispose();     
      MessageBox.Show(e.Error.Message);     
     } 

     if (e.Result != null && e.Result.Length > 0) 
     { 
      string browsetext = ""; 
      int = iSuccess = 0; 
      browsetext = Encoding.Default.GetString(e.Result); 

        iSuccess = browsetext.IndexOf("Sign up") + 1; 
        if (iSuccess == 0) 
        { 

        } 
        else 
        { 

         listBox1.Items.Add(st); 
         domaincount++;        
         lblDomainCount.ForeColor = Color.DarkGreen; 
         lblDomainCount.Text = domaincount.ToString(); 
        } 
       } 
       else 
       { 
       } 
      } 
     } 
     else 
     { 
      MessageBox.Show("No data found."); 
     } 
    } 

이 도와주세요 다음 GUI를 끊지 않는 웹 클라이언트의 대체가 있으면 제안 해주십시오 . 네.

답변

0

다운로드를 시작하자마자 WebClient를 처분합니다.

asyncWebRequest.DownloadDataAsync(urlToRequest); 
asyncWebRequest.Dispose(); 

도움을 끊지 않는 웹 클라이언트의 대체가있을 경우 GUI

비동기/await를 사용할 수 있도록 웹 클라이언트에 대한 래퍼를 생성 my other answer를 참조하십시오 . HttpClient도 대안이 될 수 있습니다.

+0

그래서 처분하면 안됩니까? – Noobish

+1

@ user1445345 다운로드가 완료된 후에 만 – I4V

관련 문제