Exemple #1
0
        private void ProcessQueue()
        {
            if (downloadQueue.Count > 0)
            {
                var downloadParams = (DownloadParams)downloadQueue.Dequeue();
                CurrentDownloadFile = SavePath + Helpers.GetDirectorySeparator() + downloadParams.FileName;
                if (AsyncMode)
                {
                    wc.DownloadFileAsync(new Uri(downloadParams.ToString()), CurrentDownloadFile);
                }
                else
                {
                    wc.DownloadFile(new Uri(downloadParams.ToString()), CurrentDownloadFile);

                    // since we are in blocking mode we have to set success explicitly
                    DownloadedFiles.Last().Success = true;
                    ProcessQueue();
                }
            }
            else
            {
                if (OnDownloadAllCompletedEvent != null)
                {
                    OnDownloadAllCompletedEvent(this, new EventArgs());
                }
            }
        }
        private void ProcessQueue()
        {
            if (downloadQueue.Count > 0)
            {
                var downloadParams = (DownloadParams)downloadQueue.Dequeue();
                int count          = downloadCount - downloadQueue.Count;

                CurrentDownloadFile = SavePath + Helpers.GetDirectorySeparator() + downloadParams.FileName;

                if (AsyncMode)
                {
                    wc.DownloadFileAsync(new Uri(downloadParams.ToString()), CurrentDownloadFile);
                }
                else
                {
                    try
                    {
                        float progress = count > 0 ? ((float)count / (float)downloadCount) * 100f : 0f;
                        Console.Write($"Downloading {Path.GetFileNameWithoutExtension(CurrentDownloadFile)} [{count}/{downloadCount}] ({progress.ToString("0")}%) ... ");
                        wc.DownloadFile(new Uri(downloadParams.ToString()), CurrentDownloadFile);

                        // since we are in blocking mode we have to set success explicitly
                        DownloadedFiles.Last().Success = true;
                        Console.WriteLine($"SUCCESS!");
                    }
                    catch (WebException ex)
                    {
                        Console.WriteLine($"\nERROR: {ex.Message} ({Path.GetFileNameWithoutExtension(CurrentDownloadFile)})");

                        if (string.IsNullOrEmpty(errorLogs.Where(x => x.Contains(Path.GetFileNameWithoutExtension(CurrentDownloadFile))).FirstOrDefault()))
                        {
                            errorLogs.Add(Path.GetFileNameWithoutExtension(CurrentDownloadFile) + " - " + ex.Message);
                        }
                    }

                    ProcessQueue();
                }
            }
            else
            {
                if (OnDownloadAllCompletedEvent != null)
                {
                    OnDownloadAllCompletedEvent(this, new EventArgs());
                }

                errorLogs = errorLogs.OrderBy(x => x).ToList();
                File.WriteAllLines("logs.txt", errorLogs.ToArray());
            }
        }