public void ScheduleFile(string fileName)
 {
     if (this._downloadList.ContainsKey(fileName))
     {
         DownloaderHandler.DownloadStatus status = DownloaderHandler.DownloadStatus.Queued;
         lock (this._downloadList[fileName])
         {
             status = this._downloadList[fileName].Status;
         }
         if (status != DownloaderHandler.DownloadStatus.Queued && status != DownloaderHandler.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 = DownloaderHandler.DownloadStatus.Queued;
         }
     }
     else
     {
         this._downloadList.Add(fileName, new DownloaderHandler.DownloadItem());
         lock (this._downloadQueue)
         {
             this._downloadQueue.AddLast(fileName);
         }
     }
     if (this._managerRunning && DownloaderHandler._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);
             DownloaderHandler._workerCount++;
         }
     }
 }
 public DownloadItem()
 {
     this.Status = DownloaderHandler.DownloadStatus.Queued;
 }
 private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs args)
 {
     try
     {
         if (WebCalls.Alternative())
         {
             using (WebClient webClient = new WebClient())
             {
                 webClient.Headers.Add("user-agent", "SBRW Launcher " +
                                       Application.ProductVersion + " (+https://github.com/SoapBoxRaceWorld/GameLauncher_NFSW)");
                 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);
                                 }
                                 DownloaderHandler._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 != DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 this._downloadList[value].Status = DownloaderHandler.DownloadStatus.Downloading;
                             }
                         }
                         while (webClient.IsBusy)
                         {
                             Thread.Sleep(100);
                         }
                         webClient.DownloadDataAsync(new Uri(value), value);
                         DownloaderHandler.DownloadStatus status = DownloaderHandler.DownloadStatus.Downloading;
                         while (status == DownloaderHandler.DownloadStatus.Downloading)
                         {
                             status = this._downloadList[value].Status;
                             if (status == DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 break;
                             }
                             Thread.Sleep(100);
                         }
                         if (status == DownloaderHandler.DownloadStatus.Canceled)
                         {
                             webClient.CancelAsync();
                         }
                         lock (this._workers)
                         {
                             if (DownloaderHandler._workerCount > this._maxWorkers || !this._managerRunning)
                             {
                                 this._workers.Remove((BackgroundWorker)sender);
                                 DownloaderHandler._workerCount--;
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         else
         {
             using (WebClientWithTimeout webClient = new WebClientWithTimeout())
             {
                 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);
                                 }
                                 DownloaderHandler._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 != DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 this._downloadList[value].Status = DownloaderHandler.DownloadStatus.Downloading;
                             }
                         }
                         while (webClient.IsBusy)
                         {
                             Thread.Sleep(100);
                         }
                         webClient.DownloadDataAsync(new Uri(value), value);
                         DownloaderHandler.DownloadStatus status = DownloaderHandler.DownloadStatus.Downloading;
                         while (status == DownloaderHandler.DownloadStatus.Downloading)
                         {
                             status = this._downloadList[value].Status;
                             if (status == DownloaderHandler.DownloadStatus.Canceled)
                             {
                                 break;
                             }
                             Thread.Sleep(100);
                         }
                         if (status == DownloaderHandler.DownloadStatus.Canceled)
                         {
                             webClient.CancelAsync();
                         }
                         lock (this._workers)
                         {
                             if (DownloaderHandler._workerCount > this._maxWorkers || !this._managerRunning)
                             {
                                 this._workers.Remove((BackgroundWorker)sender);
                                 DownloaderHandler._workerCount--;
                                 break;
                             }
                         }
                     }
                 }
             }
         }
     }
     catch (Exception Error)
     {
         LogToFileAddons.OpenLog("CDN DOWNLOADER [Background Work DH]", null, Error, null, true);
         Exception exception = Error;
         lock (this._workers)
         {
             this._workers.Remove((BackgroundWorker)sender);
             DownloaderHandler._workerCount--;
         }
     }
 }