public DownloadControl(VideoQueueItem video) { InitializeComponent(); lblFilename.Text = video.VideoFileName; Thread thread = new Thread(() => { WebClient client = new WebClient(); client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); client.DownloadFileAsync(new Uri(video.VideoURL), video.DestinationFile); }); thread.Start(); // On completed: OnCompleted?.Invoke(); }
private void DownloaderTimer_Tick(object sender, EventArgs e) { if (VideoQueue.Count > 0 && DownloadsInProgress < MaxDownloads) { VideoQueueItem video = VideoQueue.Dequeue(); if (CompletedDownloads.ContainsKey(video.VideoURL)) { return; } CompletedDownloads.Add(video.VideoURL, video); DownloadsInProgress++; /* * Thread thread = new Thread(() => { * WebClient client = new WebClient(); * client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); * client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); * client.DownloadFileAsync(new Uri(video.VideoURL), video.DestinationFile); * }); * thread.Start(); */ this.BeginInvoke((MethodInvoker) delegate { DownloadControl dc = new DownloadControl(video); dc.OnCompleted += () => { DownloadsInProgress--; downloadPanel.Controls.Remove(dc); }; downloadPanel.Controls.Add(dc); }); } }