private void CancelDownload(uint AppID, uint DepotID, string Branch, DownloadProgressBar progressBar)
 {
     foreach (ContentDownloader downloader in Program.DownloaderInstances)
     {
         if (downloader.Config.AppID == AppID &&
             downloader.Config.DepotID == DepotID &&
             downloader.Config.Branch == Branch)
         {
             downloader.StopDownload();
             Program.DownloaderInstances.Remove(downloader);
             this.panelDownloading.Controls.Remove(progressBar);
             foreach (DownloadRecord Dr in ConfigStore.TheConfig.DownloadRecord)
             {
                 if (Dr.AppID == AppID && Dr.DepotID == DepotID && Dr.BranchName == Branch)
                 {
                     ConfigStore.TheConfig.DownloadRecord.Remove(Dr);
                     ConfigStore.Save();
                     break;
                 }
             }
             RegroupDownloadProgressControl();
             return;
         }
     }
 }
        public void CreateDownloadTask(string DownloadName, DownloadConfig Dc, bool AdvancedConfig, bool IsRestore)
        {
            DownloadProgressBar Dpb = new DownloadProgressBar();

            Dc.OnReportProgressEvent   += Dpb.OnDownloadProgress;
            Dc.OnDownloadFinishedEvent += Dpb.OnDownloadFinished;
            Dc.OnStateChangedEvent     += Dpb.OnStateChanged;
            var TargetDownloader = new ContentDownloader();

            Program.DownloaderInstances.Add(TargetDownloader);
            Dpb.RestartDownload          += TargetDownloader.RestartDownload;
            Dpb.StopDownload             += TargetDownloader.StopDownload;
            Dpb.CancelDownload           += this.CancelDownload;
            Dpb.OnDownloadFinishedReport += this.OnDownloadFinished;
            Dpb.InitDownloading(DownloadName, Dc.AppID, Dc.DepotID, Dc.Branch);
            this.panelDownloading.Controls.Add(Dpb);
            RegroupDownloadProgressControl();
            if (!IsRestore)
            {
                DownloadRecord Dr = new DownloadRecord();
                Dr.AppID          = Dc.AppID;
                Dr.DepotID        = Dc.DepotID;
                Dr.BranchName     = Dc.Branch;
                Dr.FileToDownload = AllowFileList;
                Dr.InstallDir     = InstallDir;
                Dr.NoForceDepot   = !Dc.ForceDepot;
                Dr.DownloadName   = DownloadName;
                Dr.MaxDownload    = Dc.MaxDownloads;
                Dr.MaxServer      = Dc.MaxServers;
                Dr.AllPlatforms   = Dc.DownloadAllPlatforms;
                Dr.AdvancedConfig = AdvancedConfig;
                Dr.ManifestID     = Dc.ManifestId;
                if (Dc.FilesToDownloadRegex != null)
                {
                    Dr.FileRegex = new List <string>();
                    foreach (System.Text.RegularExpressions.Regex Fileregex in Dc.FilesToDownloadRegex)
                    {
                        Dr.FileRegex.Add(Fileregex.ToString());
                    }
                }
                ConfigStore.TheConfig.DownloadRecord.Add(Dr);
                ConfigStore.Save();
            }
            TargetDownloader.Config = Dc;
            if (!IsRestore)
            {
                TargetDownloader.RestartDownload();
            }
            else
            {
                Dpb.Downloading = false;
            }
        }