public void Start() { if (!CanStart) { return; } IsActive = true; IsSuccessful = false; IsCanceled = false; IsFailed = false; Task.Run(async() => { // Create cancellation token source _cancellationTokenSource = new CancellationTokenSource(); // Create progress operation ProgressOperation = ProgressManager.CreateOperation(); try { // If download option is not set - get the best download option if (DownloadOption == null) { DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format); } await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token); if (_settingsService.ShouldInjectTags) { await _taggingService.InjectTagsAsync(Video, Format, FilePath, _cancellationTokenSource.Token); } if (SubtitleOption != null && SubtitleOption.ClosedCaptionTrackInfos.Any()) { await _downloadService.DownloadSubtitleAsync(SubtitleOption, FilePath); } IsSuccessful = true; } catch (OperationCanceledException) { IsCanceled = true; } catch (Exception ex) { IsFailed = true; FailReason = ex.Message; } finally { IsActive = false; _cancellationTokenSource.Dispose(); ProgressOperation.Dispose(); } }); }
public void Start() { if (!CanStart) { return; } IsActive = true; IsSuccessful = false; IsCanceled = false; IsFailed = false; Task.Run(async() => { // Create cancellation token source _cancellationTokenSource = new CancellationTokenSource(); // Create progress operation ProgressOperation = ProgressManager.CreateOperation(); try { // daca nu sunt setate obtiuni de descarcare se foloseste setarea de baza // descarca video if (DownloadOption == null) { DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format); } await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token); if (_settingsService.ShouldInjectTags) { await _taggingService.InjectTagsAsync(Video, Format, FilePath, _cancellationTokenSource.Token); } IsSuccessful = true; } catch (OperationCanceledException) { IsCanceled = true; } catch (Exception ex) { IsFailed = true; FailReason = ex.Message; } finally { IsActive = false; _cancellationTokenSource.Dispose(); ProgressOperation.Dispose(); } }); }
/// <summary> /// Runs an operation asynchronously, with optional progress bar. /// </summary> /// <param name="operation"></param> /// <param name="showProgressWindow"></param> /// <returns></returns> static public async Task RunOperationAsync(ProgressOperation operation) { try { await Task.Run((Action)operation.Run); } catch (Exception e) { Log(e.ToString()); } finally { operation.Dispose(); } } // RunOperationAsync()
public void ProgressOperation_Report_Disposed_Test() { // Create operation var operation = new ProgressOperation(); // Report completion operation.Dispose(); // It shouldn't be possible to report progress anymore Assert.Throws <InvalidOperationException>(() => operation.Report(0.5)); }
public void Start() { if (!CanStart) { return; } Task.Run(async() => { // Create cancellation token source _cancellationTokenSource = new CancellationTokenSource(); // Create progress operation ProgressOperation = ProgressManager.CreateOperation(); try { IsSuccessful = false; IsCanceled = false; IsFailed = false; IsActive = true; // If download option is not set - get the best download option if (DownloadOption == null) { DownloadOption = await _downloadService.GetBestDownloadOptionAsync(Video.Id, Format); } // Download await _downloadService.DownloadVideoAsync(DownloadOption, FilePath, ProgressOperation, _cancellationTokenSource.Token); IsSuccessful = true; } catch (OperationCanceledException) { IsCanceled = true; } catch (Exception ex) { IsFailed = true; FailReason = ex.Message; } finally { IsActive = false; _cancellationTokenSource.Dispose(); ProgressOperation.Dispose(); } }); }
public void ProgressOperation_Dispose_Test() { // Create operation var operation = new ProgressOperation(); // Assert initial state Assert.That(operation.IsCompleted, Is.False); // Report completion operation.Dispose(); // Assert final state Assert.That(operation.IsCompleted, Is.True); }
public void ProgressOperation_NotifyPropertyChanged_Test() { // Create operation var operation = new ProgressOperation(); // Subscribe to event var eventTriggerCount = 0; operation.PropertyChanged += (sender, args) => eventTriggerCount++; // Invoke changes operation.Report(0.1); operation.Report(0.3); operation.Dispose(); // Assert that the event was triggered accordingly Assert.That(eventTriggerCount, Is.GreaterThanOrEqualTo(3)); }
public void MarkAsCompleted() { _cancellationTokenSource.Dispose(); ProgressOperation.Dispose(); IsCompleted = true; }
public void Start() { if (!CanStart) { return; } IsActive = true; IsSuccessful = false; IsCanceled = false; IsFailed = false; Task.Run(async() => { _cancellationTokenSource = new CancellationTokenSource(); ProgressOperation = ProgressManager?.CreateOperation(); try { // If download option is not set - get the best download option VideoOption ??= await _downloadService.TryGetBestVideoDownloadOptionAsync( Video.Id, Format, QualityPreference ); // It's possible that video has no streams if (VideoOption == null) { throw new InvalidOperationException($"Video '{Video.Id}' contains no streams."); } await _downloadService.DownloadAsync( VideoOption, SubtitleOption, FilePath, ProgressOperation, _cancellationTokenSource.Token ); if (_settingsService.ShouldInjectTags) { await _taggingService.InjectTagsAsync( Video, Format, FilePath, _cancellationTokenSource.Token ); } IsSuccessful = true; } catch (OperationCanceledException) { IsCanceled = true; } catch (Exception ex) { IsFailed = true; // Short error message for expected errors, full for unexpected FailReason = ex is YoutubeExplodeException ? ex.Message : ex.ToString(); } finally { IsActive = false; _cancellationTokenSource?.Dispose(); _cancellationTokenSource = null; ProgressOperation?.Dispose(); } }); }