2010-02-15 3 views
2

파일 다운로드시 이벤트가 실행되지 않는 이유는 약간 혼란 스럽습니다.DownloadFileCompleted 이벤트가 loop while while fire C# .net

파일 자체를 완벽하게 다운로드합니다.

나는 이벤트를 루프 안에서 발생시키지 않는다는 점에서 내가 사용하고있는 방식에 약간의 에러가 있다고 가정하고있다. 어떤 도움 누구를위한

덕분에 어디() 완료 호출 DownloadFileAsync 기다리고있는 나에게

class DownloadQueue 
{ 
    public List<string[]> DownloadItems { get; set; } 
    public int CurrentDownloads; 
    public int DownloadInProgress; 
    string url = @"http://www.google.co.uk/intl/en_uk/images/logo.gif"; 
    bool downloadComplete; 

    public DownloadQueue() 
    { 
     CurrentDownloads = 0; 
     DownloadItems = new List<string[]>(); 
     Console.Write("new download queue made"); 
    } 

    public void startDownloading(int maxSimulatiousDownloads) 
    { 
     downloadComplete = true; 
     DownloadInProgress = 0; 
     WebClient client = new WebClient(); 
     client.DownloadFileCompleted += 
        new AsyncCompletedEventHandler(this.downloadCompleteMethod); 


      while(DownloadInProgress != DownloadItems.Count) 
      { 
       if (downloadComplete == true) 
       { 
        downloadComplete = false;   

        client.DownloadFileAsync(new Uri(DownloadItems.ElementAt(DownloadInProgress).ElementAt(0).ToString()), DownloadItems.ElementAt(DownloadInProgress).ElementAt(1).ToString());       
       } 
      } 
     Console.Write("all downloads completed"); 
    } 

    private void downloadCompleteMethod(object sender, AsyncCompletedEventArgs e) 
    {    
     downloadComplete = true; 
     DownloadInProgress++; 
     Console.Write("file Downloaded"); 
    } 

    } 

답변

-1

을 줄 수 있습니까? 어떻게

manualResetEvent AllDone = new mre(false) 

같은 약 단지 console.WriteLine

AllDone.WaitOne() 

if (interlocked.decrement(ref downloadComplete) == 0) { AllDone.Set(); } 
-1

메시지가 와서 할 시간이없는 대부분의 경우 전체 다운로드한다. 루프 끝 부분에 Application.DoEvents를두면 이벤트가 실행되기 시작합니다. 그것을 사용하는 것이 이상적이지는 않지만, 가지고있는 디자인으로는 더 좋은 방법을 생각할 수 없습니다.