public DoDownloadArg(OneSeriesDownloadInfo di, CommnonDoDownloadInfo commonInfo, Thread downloadThread) { DownloadInfo = di; CommnonInfo = commonInfo; DownloadThread = downloadThread; }
void _worker_DoWork(object sender, DoWorkEventArgs e) { _worker.ReportProgress(0, "Connecting to server..."); _savedData.Clear(); _savedData.Capacity = Information.StartArgs.ItemsToDownload.Count; var downloadList = Information.StartArgs.ItemsToDownload; var indeces = Information.IndecesToDownload; // max count of downloading threads int maxThreadsToDownloadCount = 16; if (Information.StartArgs.DownloadOptions.UseSingleThread) { maxThreadsToDownloadCount = 1; } var commonInfo = new CommnonDoDownloadInfo(new Downloader(Information.StartArgs.DownloadOptions)); // common info, shared through downloading threads // Starting (if possible) maxThreadsToDownloadCount downloading threads for (int i = 0, startedThreadsCount = 0; startedThreadsCount < maxThreadsToDownloadCount && i < indeces.Count; i++) { var thread = new Thread(DoDownload); var dda = new DoDownloadArg(downloadList[indeces[i]], commonInfo, thread); commonInfo.AddDonwloadingThread(thread); commonInfo.LastDownloadingIndex++; thread.Start(dda); startedThreadsCount++; } // Starting save thread var saveThread = new Thread(DoSave); saveThread.Start(commonInfo); // Blocking current thread until all the series will not saved, or downloading cancelled commonInfo.SavingWaitingEvent = new ManualResetEvent(false); commonInfo.SavingWaitingEvent.WaitOne(); if (_worker.CancellationPending) { e.Cancel = true; } // waiting all downloading threads while (commonInfo.DownloadingThreadsCount > 0) { } #region Final logging var sb = new StringBuilder(Environment.NewLine); sb.AppendLine("==============="); sb.AppendLine("Total:"); sb.AppendLine("ServiceURL SiteCode VariableCode StartDate EndDate DownloadTime Status ErrorMessage"); foreach (var ind in indeces) { var di = downloadList[ind]; sb.AppendFormat("{0} {1} {2} {3} {4} {5} {6} {7}" + Environment.NewLine, di.Wsdl, di.FullSiteCode, di.FullVariableCode, di.StartDate, di.EndDate, di.DownloadTimeTaken, di.Status, di.ErrorMessage); } sb.AppendLine("==============="); DoLogInfo(sb.ToString()); #endregion _worker.ReportProgress(100, e.Cancel? "Download cancelled." : "Download Complete."); }