public void ScheduleFile(string fileName)
 {
     if (this._downloadList.ContainsKey(fileName))
     {
         DownloadManager.DownloadStatus status = DownloadManager.DownloadStatus.Queued;
         lock (this._downloadList[fileName])
         {
             status = this._downloadList[fileName].Status;
         }
         if (status != DownloadManager.DownloadStatus.Queued && status != DownloadManager.DownloadStatus.Canceled)
         {
             return;
         }
         lock (this._downloadQueue)
         {
             if (this._downloadQueue.Contains(fileName) && this._downloadQueue.Last.Value != fileName)
             {
                 this._downloadQueue.Remove(fileName);
                 this._downloadQueue.AddLast(fileName);
             }
             else if (!this._downloadQueue.Contains(fileName))
             {
                 this._downloadQueue.AddLast(fileName);
             }
         }
         lock (this._downloadList[fileName])
         {
             this._downloadList[fileName].Status = DownloadManager.DownloadStatus.Queued;
         }
     }
     else
     {
         this._downloadList.Add(fileName, new DownloadManager.DownloadItem());
         lock (this._downloadQueue)
         {
             this._downloadQueue.AddLast(fileName);
         }
     }
     if (this._managerRunning && DownloadManager._workerCount < this._maxWorkers)
     {
         lock (this._workers)
         {
             BackgroundWorker backgroundWorker = new BackgroundWorker();
             backgroundWorker.DoWork             += new DoWorkEventHandler(this.BackgroundWorker_DoWork);
             backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.BackgroundWorker_RunWorkerComplete);
             backgroundWorker.RunWorkerAsync();
             this._workers.Add(backgroundWorker);
             DownloadManager._workerCount++;
         }
     }
 }
 private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs args)
 {
     try
     {
         using (WebClient webClient = new WebClient())
         {
             webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(this.DownloadManager_DownloadDataCompleted);
             webClient.CachePolicy            = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
             while (true)
             {
                 if (this._freeChunks <= 0)
                 {
                     Thread.Sleep(100);
                 }
                 else
                 {
                     lock (this._downloadQueue)
                     {
                         if (this._downloadQueue.Count == 0)
                         {
                             lock (this._workers)
                             {
                                 this._workers.Remove((BackgroundWorker)sender);
                             }
                             DownloadManager._workerCount--;
                             break;
                         }
                     }
                     string value = null;
                     lock (this._downloadQueue)
                     {
                         value = this._downloadQueue.Last.Value;
                         this._downloadQueue.RemoveLast();
                         lock (this._freeChunksLock)
                         {
                             this._freeChunks--;
                         }
                     }
                     lock (this._downloadList[value])
                     {
                         if (this._downloadList[value].Status != DownloadManager.DownloadStatus.Canceled)
                         {
                             this._downloadList[value].Status = DownloadManager.DownloadStatus.Downloading;
                         }
                     }
                     while (webClient.IsBusy)
                     {
                         Thread.Sleep(100);
                     }
                     webClient.DownloadDataAsync(new Uri(value), value);
                     DownloadManager.DownloadStatus status = DownloadManager.DownloadStatus.Downloading;
                     while (status == DownloadManager.DownloadStatus.Downloading)
                     {
                         status = this._downloadList[value].Status;
                         if (status == DownloadManager.DownloadStatus.Canceled)
                         {
                             break;
                         }
                         Thread.Sleep(100);
                     }
                     if (status == DownloadManager.DownloadStatus.Canceled)
                     {
                         webClient.CancelAsync();
                     }
                     lock (this._workers)
                     {
                         if (DownloadManager._workerCount > this._maxWorkers || !this._managerRunning)
                         {
                             this._workers.Remove((BackgroundWorker)sender);
                             DownloadManager._workerCount--;
                             break;
                         }
                     }
                 }
             }
         }
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         lock (this._workers)
         {
             this._workers.Remove((BackgroundWorker)sender);
             DownloadManager._workerCount--;
         }
     }
 }
 public DownloadItem()
 {
     this.Status = DownloadManager.DownloadStatus.Queued;
 }