/// <summary> /// Запустить закачки /// </summary> public void Run() { int count = mDownloaders.Count(); int ok_count = 0; int error_count = 0; Console.WriteLine("Links found: {0}", count); using (DownloaderView view = new DownloaderView()) { int count_left = mDownloaders.Count(); view.SetMaxNumber(count_left); int count_current = 0; while (count_left > 0) { for (int i = 0; i < mDownloaders.Count(); ++i) { Downloader downloader = mDownloaders[i]; DownloadInfo info = downloader.GetInfo(); if (!info.IsStarted() && count_current < mMaxParallel) { downloader.DownloadAsync(); ++count_current; } else if (info.IsFinished()) { view.UpdateInfo(info); if (info.mError == "") { ++ok_count; } else { ++error_count; } --count_left; --count_current; mDownloaders.RemoveAt(i); --i; continue; } else if (info.mDownloadStatus == DownloadStatus.Started) { view.UpdateInfo(info); } } Thread.Sleep(mSleepMsec); } } Console.WriteLine("Downloaded: {0}, Error: {1}", ok_count, error_count); }
/// <summary> /// Обновить информацию о конкретной закачке /// </summary> public void UpdateInfo(DownloadInfo info) { if (info.mDownloadStatus == DownloadStatus.NotStarted) { return; // Не выводим инфу о неначатых } int progressStringSize = (10 * 2 + 3 + 2); var paintedString = new PaintedConsoleString(); paintedString.Append(info.mNumber.ToString(mNumberFormat)); paintedString.Append("/"); paintedString.Append(mMaxNumber.ToString(mNumberFormat)); paintedString.Append(" "); int count_progress_symbols = 0; string progress_string = ""; ConsoleColor colorForProgress = Console.BackgroundColor; if (info.mDownloadStatus == DownloadStatus.Started) { StringBuilder sb_prog = new StringBuilder(); if (info.mBytes > 0) { string left = Helpers.FormatBytes(info.mDownloadedBytes); string center = " "; string right = Helpers.FormatBytes(info.mBytes); progress_string = ProgressStringAligner(left, center, right, progressStringSize); count_progress_symbols = calcProgressSymbols( info.mDownloadedBytes, info.mBytes, progress_string.Count()); } else { progress_string = ProgressStringAligner(" ... ", progressStringSize); count_progress_symbols = calcProgressSymbols(0, 1, progress_string.Count()); } colorForProgress = ConsoleColor.DarkGray; } else if (info.mDownloadStatus == DownloadStatus.Finished) { if (info.mError != "") { string str_in_prog; if (info.mHttpErrorCode != 0) { str_in_prog = info.mHttpErrorCode.ToString(); } else { str_in_prog = "ERROR"; } colorForProgress = ConsoleColor.DarkRed; progress_string = ProgressStringAligner(str_in_prog, progressStringSize); } else { string str_in_prog = Helpers.FormatBytes(info.mBytes); colorForProgress = ConsoleColor.DarkGreen; progress_string = ProgressStringAligner("", "", str_in_prog, progressStringSize); } count_progress_symbols = calcProgressSymbols(1, 1, progress_string.Count()); } int start_progress = paintedString.SetColor(colorForProgress); paintedString.Append(progress_string); paintedString.SetDefaultColor(start_progress + count_progress_symbols); paintedString.Append(" "); paintedString.Append(info.GetFileCaption()); mConsole.WriteLine(info.mNumber, paintedString); }