代码之家  ›  专栏  ›  技术社区  ›  Chris Barry

DownloadFileCompleted事件在循环c#.net时不会在内部触发

  •  2
  • Chris Barry  · 技术社区  · 15 年前

    它自己下载的文件非常好。

    谢谢任何人能给我的帮助

    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");
        }
    
        }
    
    2 回复  |  直到 15 年前
        1
  •  -1
  •   Nick    15 年前

    manualResetEvent AllDone = new mre(false)
    

    就在console.WriteLine之前

    AllDone.WaitOne()
    

    if (interlocked.decrement(ref downloadComplete) == 0) { AllDone.Set(); }
    
        2
  •  -1
  •   Rick Mogstad    15 年前

    很可能没有时间来传达信息。我怀疑如果你把Application.DoEvents放在循环的末尾,它就会开始触发事件。用起来不太理想,但以你的设计,我想不出更好的办法了。