private async void Video() { try { IsVideoDownloading = true; MP3ButtonEnabled = false; VideoButtonEnabled = false; await _YoutubeClient.DownloadHighestVideo(VideoUrl, _VideoProgress, VideoDownloadInfo, null, null, CancellationToken.None); } catch (HttpRequestException ex) { MessageBox.Show("Fehler beim Verbindungsaufbau" + Environment.NewLine + ex.Message + (ex.InnerException == null ? Environment.NewLine + ex.InnerException.Message : "")); } catch (ArgumentException ex) { MessageBox.Show(ex.Message, "Ungültiger Parameter"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { MP3ButtonEnabled = true; VideoButtonEnabled = true; IsVideoDownloading = false; VideoPercentage = 0; } }
private async void Work() { if (isRunning) { if (!cancellationTokenSource.IsCancellationRequested) { if (MessageBox.Show("Do u want to cancel the download?", "", MessageBoxButtons.YesNo) == DialogResult.Yes) { cancellationTokenSource.Cancel(); } } return; } if (string.IsNullOrEmpty(textBoxVideoId.Text)) { return; } if (!ValidateSettings()) { return; } buttonDownload.Text = "cancel"; var progress = new Progress <double>(x => { if (this.IsDisposed) { return; } toolStripProgressBar.Value = (int)x; toolStripStatusLabel.Text = $"downloading {x.ToString("00.00")}%"; }); try { isRunning = true; YoutubeClient = new YoutubeClient(Settings.FFmpegLocation, Settings.DownloadLocation, progress, Settings.OverwriteFiles, cancellationTokenSource.Token); YoutubeClient.VideoDownloadInfo += this.YoutubeClient_VideoDownloadInfo; YoutubeClient.MuxingStarted += (s, e) => this.Invoke(() => toolStripStatusLabel.Text = "muxing started"); YoutubeClient.MuxingFinished += (s, e) => this.Invoke(() => toolStripStatusLabel.Text = "muxing finished"); await YoutubeClient.DownloadHighestVideo(textBoxVideoId.Text); } catch (ArgumentException ex) when(ex.ParamName == "videoId") { MessageBox.Show("Invalid video id"); } catch (TaskCanceledException) when(cancellationTokenSource.IsCancellationRequested) { } catch (Exception ex) { MessageBox.Show(ex.Message, "An error ocurred"); } finally { isRunning = false; YoutubeClient = null; cancellationTokenSource = new CancellationTokenSource(); if (!this.IsDisposed) { buttonDownload.Text = "download"; toolStripProgressBar.Value = 0; toolStripStatusLabel.Text = ""; labelVideoTitelValue.Text = ""; labelAudioSizeValue.Text = ""; labelVideoSizeValue.Text = ""; labelTotalSizeValue.Text = ""; } } }