2012-12-15 2 views
0

을 완료 할 때까지이 내 코드입니다 :기다립니다 다운로드

Action<int, ProgressBar, Label, Label, int, Button> downloadFileAsync = (i, pb, label2, label1, ServID, button1) => 
    { 
     var bd = AppDomain.CurrentDomain.BaseDirectory; 
     var fn = bd + "/" + i + ".7z"; 
     var down = new WebClient(); 
     DownloadProgressChangedEventHandler dpc = (s, e) => 
     { 
      label1.Text = "Download Update: " + i + "/" + ServID; 
      int rec =Convert.ToInt16(e.BytesReceived/1024); 
      int total =Convert.ToInt16(e.TotalBytesToReceive/1024) ; 
      label2.Text = "Downloaded: " + rec.ToString() + "KB/" + total.ToString() + " KB"; 
      pb.Value = e.ProgressPercentage; 
     }; 
     AsyncCompletedEventHandler dfc = null; dfc = (s, e) => 
     { 
      label1.Text = "Extracting Files..."; 
      Rar Ra = new Rar(); 
      Ra.Open(i + ".7z"); 
      Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory); 
      File.Delete(fn); 
      down.DownloadProgressChanged -= dpc; 
      down.DownloadFileCompleted -= dfc; 
      down.Dispose(); 
      if (ServID == i) 
      { 
       button1.Enabled = true; 
       label1.Text = "Have Fun In-Game..."; 
       label2.Text = "Done..."; 
      } 
     }; 
     down.DownloadProgressChanged += dpc; 
     down.DownloadFileCompleted += dfc; 
     down.DownloadFileAsync(new Uri("http://unknowndekaron.net/updatee/" + i + "/" + i + ".7z"), fn); 
    }; 

이 내 전화입니다 :

   while (i <= ServID) 
       { 
        downloadFileAsync(i, progressBar1, label2, label1, ServID, button1); 
        i++; 
       } 

압축 해제 :이 코드 프로그램 다운로드에

public void Decompress(int i) 
    { 

     Rar Ra = new Rar(); 
     Ra.Open(i + ".7z"); 
     Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory); 

    } 

모든 업데이트에서 한 번 ... 다운로드가 완료된 첫 번째 파일의 압축을 풉니 다. 한 번에 하나의 업데이트 만 다운로드 한 다음 다시 압축 해제해야합니다.

+0

무엇이 문제입니까? 이 문제를 해결하기 위해 어떤 문제를 겪고 있습니까? –

답변

0

다음을 사용하여 논리 값으로 unelegant 솔루션을 사용할 수 있습니다

당신은 새로운 async 방법으로 최고 수준의 람다를 이동해야합니다. 정상적으로 전화를 걸 수 있습니다. 그 후 boolean을 최상위 람다 시작 부분에 downloadCompleted으로 선언하고 false로 설정합니다. 그런 다음 AsyncCompletedEventHandler으로 이동하고 마지막에 downloadCompleted의 값을 true로 설정하십시오. 이는 우리가 마친 마지막 while 루프를 알릴 것입니다. 최상위 레벨 lamba의 끝은 다운로드 및 압축의 fullfillment까지 기다려야합니다. 이렇게해도 응용 프로그램의 상태가 "응답하지 않음"으로 변경되지는 않습니다.

전화 :

downloadFileAsync = (i, pb, label2, label1, ServID, button1) => 
    { 
     Download(i, pb, label2, label1, ServID, button1); 
    }; 

새로운 방법

public async Task<int> Download(...) 
    { 
     bool downloadCompleted = false; 
     var bd = AppDomain.CurrentDomain.BaseDirectory; 
     var fn = bd + "/" + i + ".7z"; 
     var down = new WebClient(); 
     DownloadProgressChangedEventHandler dpc = (s, e) => 
     { 
      label1.Text = "Download Update: " + i + "/" + ServID; 
      int rec = Convert.ToInt16(e.BytesReceived/1024); 
      int total = Convert.ToInt16(e.TotalBytesToReceive/1024); 
      label2.Text = "Downloaded: " + rec.ToString() + "KB/" + total.ToString() + " KB"; 
      pb.Value = e.ProgressPercentage; 
     }; 
     AsyncCompletedEventHandler dfc = null; dfc = (s, e) => 
     { 
      label1.Text = "Extracting Files..."; 
      Rar Ra = new Rar(); 
      Ra.Open(i + ".7z"); 
      Ra.Unrar(AppDomain.CurrentDomain.BaseDirectory); 
      File.Delete(fn); 
      down.DownloadProgressChanged -= dpc; 
      down.DownloadFileCompleted -= dfc; 
      down.Dispose(); 
      if (ServID == i) 
      { 
       button1.Enabled = true; 
       label1.Text = "Have Fun In-Game..."; 
       label2.Text = "Done..."; 
      } 
      downloadCompleted = true; 
     }; 
     down.DownloadProgressChanged += dpc; 
     down.DownloadFileCompleted += dfc; 
     down.DownloadFileAsync(new Uri("http://unknowndekaron.net/updatee/" + i + "/" + i + ".7z"), fn); 
     while (!downloadCompleted) 
      ; 
    } 

그대로 루프가있을 수 있지만 호출.

+0

공개 비동기를 위해 C# 5가 필요합니까 ?? –

+0

tnx man 내가이 일을하기 위해 mannge .. –

관련 문제