From e52dfeaabec496ffd525382c362115bfda9fb500 Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Fri, 5 Jan 2024 02:28:14 -0500 Subject: [PATCH 1/6] Add ITwitchTask.CanCancel and ITwitchTask.OutputFile --- .../TwitchTasks/ChatDownloadTask.cs | 13 +++++++++++++ TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs | 13 +++++++++++++ TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs | 13 +++++++++++++ .../TwitchTasks/ClipDownloadTask.cs | 14 +++++++++++++- TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs | 3 ++- TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs | 13 +++++++++++++ 6 files changed, 67 insertions(+), 2 deletions(-) diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs index d228c48b..ec1b9240 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs @@ -17,11 +17,18 @@ class ChatDownloadTask : ITwitchTask public ITwitchTask DependantTask { get; set; } public string TaskType { get; } = Translations.Strings.ChatDownload; public TwitchTaskException Exception { get; private set; } = new(); + public string OutputFile => DownloadOptions.Filename; + public bool CanCancel { get; private set; } = true; public event PropertyChangedEventHandler PropertyChanged; public void Cancel() { + if (!CanCancel) + { + return; + } + try { TokenSource.Cancel(); @@ -46,6 +53,12 @@ public void ChangeStatus(TwitchTaskStatus newStatus) { Status = newStatus; OnPropertyChanged(nameof(Status)); + + if (CanCancel && newStatus is TwitchTaskStatus.Canceled or TwitchTaskStatus.Failed or TwitchTaskStatus.Finished or TwitchTaskStatus.Stopping) + { + CanCancel = false; + OnPropertyChanged(nameof(CanCancel)); + } } public async Task RunAsync() diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs index 42b375b2..11bc2731 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs @@ -17,11 +17,18 @@ class ChatRenderTask : ITwitchTask public ITwitchTask DependantTask { get; set; } public string TaskType { get; } = Translations.Strings.ChatRender; public TwitchTaskException Exception { get; private set; } = new(); + public string OutputFile => DownloadOptions.OutputFile; + public bool CanCancel { get; private set; } = true; public event PropertyChangedEventHandler PropertyChanged; public void Cancel() { + if (!CanCancel) + { + return; + } + try { TokenSource.Cancel(); @@ -65,6 +72,12 @@ public void ChangeStatus(TwitchTaskStatus newStatus) { Status = newStatus; OnPropertyChanged(nameof(Status)); + + if (CanCancel && newStatus is TwitchTaskStatus.Canceled or TwitchTaskStatus.Failed or TwitchTaskStatus.Finished or TwitchTaskStatus.Stopping) + { + CanCancel = false; + OnPropertyChanged(nameof(CanCancel)); + } } public async Task RunAsync() diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs index 13bdd531..ecc268f8 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs @@ -17,11 +17,18 @@ class ChatUpdateTask : ITwitchTask public ITwitchTask DependantTask { get; set; } public string TaskType { get; } = Translations.Strings.ChatUpdate; public TwitchTaskException Exception { get; private set; } = new(); + public string OutputFile => UpdateOptions.OutputFile; + public bool CanCancel { get; private set; } = true; public event PropertyChangedEventHandler PropertyChanged; public void Cancel() { + if (!CanCancel) + { + return; + } + try { TokenSource.Cancel(); @@ -46,6 +53,12 @@ public void ChangeStatus(TwitchTaskStatus newStatus) { Status = newStatus; OnPropertyChanged(nameof(Status)); + + if (CanCancel && newStatus is TwitchTaskStatus.Canceled or TwitchTaskStatus.Failed or TwitchTaskStatus.Finished or TwitchTaskStatus.Stopping) + { + CanCancel = false; + OnPropertyChanged(nameof(CanCancel)); + } } public async Task RunAsync() diff --git a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs index a768cb62..1d1c5626 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs @@ -17,11 +17,18 @@ class ClipDownloadTask : ITwitchTask public ITwitchTask DependantTask { get; set; } public string TaskType { get; } = Translations.Strings.ClipDownload; public TwitchTaskException Exception { get; private set; } = new(); + public string OutputFile => DownloadOptions.Filename; + public bool CanCancel { get; private set; } = true; public event PropertyChangedEventHandler PropertyChanged; public void Cancel() { + if (!CanCancel) + { + return; + } + try { TokenSource.Cancel(); @@ -46,6 +53,12 @@ public void ChangeStatus(TwitchTaskStatus newStatus) { Status = newStatus; OnPropertyChanged(nameof(Status)); + + if (CanCancel && newStatus is TwitchTaskStatus.Canceled or TwitchTaskStatus.Failed or TwitchTaskStatus.Finished or TwitchTaskStatus.Stopping) + { + CanCancel = false; + OnPropertyChanged(nameof(CanCancel)); + } } public async Task RunAsync() @@ -56,7 +69,6 @@ public async Task RunAsync() return; } - Progress progress = new Progress(); progress.ProgressChanged += Progress_ProgressChanged; ClipDownloader downloader = new ClipDownloader(DownloadOptions, progress); diff --git a/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs b/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs index 5653627c..6445ca35 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs @@ -24,10 +24,11 @@ public interface ITwitchTask : INotifyPropertyChanged ITwitchTask DependantTask { get; set; } string TaskType { get; } TwitchTaskException Exception { get; } + string OutputFile { get; } + bool CanCancel { get; } Task RunAsync(); void Cancel(); bool CanRun(); - void ChangeStatus(TwitchTaskStatus newStatus); } } diff --git a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs index 73d1cac7..fe03c9fd 100644 --- a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs @@ -17,11 +17,18 @@ class VodDownloadTask : ITwitchTask public ITwitchTask DependantTask { get; set; } public string TaskType { get; } = Translations.Strings.VodDownload; public TwitchTaskException Exception { get; private set; } = new(); + public string OutputFile => DownloadOptions.Filename; + public bool CanCancel { get; private set; } = true; public event PropertyChangedEventHandler PropertyChanged; public void Cancel() { + if (!CanCancel) + { + return; + } + try { TokenSource.Cancel(); @@ -46,6 +53,12 @@ public void ChangeStatus(TwitchTaskStatus newStatus) { Status = newStatus; OnPropertyChanged(nameof(Status)); + + if (CanCancel && newStatus is TwitchTaskStatus.Canceled or TwitchTaskStatus.Failed or TwitchTaskStatus.Finished or TwitchTaskStatus.Stopping) + { + CanCancel = false; + OnPropertyChanged(nameof(CanCancel)); + } } public async Task RunAsync() From 5140eed4b9d53601832888b2c093a2de6605afff Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Fri, 5 Jan 2024 02:34:00 -0500 Subject: [PATCH 2/6] Remove now redundant try-catch --- TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs | 7 ++----- TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs | 7 ++----- TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs | 7 ++----- TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs | 7 ++----- TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs | 7 ++----- 5 files changed, 10 insertions(+), 25 deletions(-) diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs index ec1b9240..52c610e6 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs @@ -29,11 +29,7 @@ public void Cancel() return; } - try - { - TokenSource.Cancel(); - } - catch (ObjectDisposedException) { } + TokenSource.Cancel(); if (Status == TwitchTaskStatus.Running) { @@ -66,6 +62,7 @@ public async Task RunAsync() if (TokenSource.IsCancellationRequested) { TokenSource.Dispose(); + ChangeStatus(TwitchTaskStatus.Canceled); return; } diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs index 11bc2731..4af99dfd 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs @@ -29,11 +29,7 @@ public void Cancel() return; } - try - { - TokenSource.Cancel(); - } - catch (ObjectDisposedException) { } + TokenSource.Cancel(); if (Status == TwitchTaskStatus.Running) { @@ -85,6 +81,7 @@ public async Task RunAsync() if (TokenSource.IsCancellationRequested) { TokenSource.Dispose(); + ChangeStatus(TwitchTaskStatus.Canceled); return; } diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs index ecc268f8..2b10c7fb 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs @@ -29,11 +29,7 @@ public void Cancel() return; } - try - { - TokenSource.Cancel(); - } - catch (ObjectDisposedException) { } + TokenSource.Cancel(); if (Status == TwitchTaskStatus.Running) { @@ -66,6 +62,7 @@ public async Task RunAsync() if (TokenSource.IsCancellationRequested) { TokenSource.Dispose(); + ChangeStatus(TwitchTaskStatus.Canceled); return; } diff --git a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs index 1d1c5626..274f4565 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs @@ -29,11 +29,7 @@ public void Cancel() return; } - try - { - TokenSource.Cancel(); - } - catch (ObjectDisposedException) { } + TokenSource.Cancel(); if (Status == TwitchTaskStatus.Running) { @@ -66,6 +62,7 @@ public async Task RunAsync() if (TokenSource.IsCancellationRequested) { TokenSource.Dispose(); + ChangeStatus(TwitchTaskStatus.Canceled); return; } diff --git a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs index fe03c9fd..95f64074 100644 --- a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs @@ -29,11 +29,7 @@ public void Cancel() return; } - try - { - TokenSource.Cancel(); - } - catch (ObjectDisposedException) { } + TokenSource.Cancel(); if (Status == TwitchTaskStatus.Running) { @@ -66,6 +62,7 @@ public async Task RunAsync() if (TokenSource.IsCancellationRequested) { TokenSource.Dispose(); + ChangeStatus(TwitchTaskStatus.Canceled); return; } From b3b74d23e45ea7cafd30fbddd75923fa628ba58e Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Fri, 5 Jan 2024 02:36:08 -0500 Subject: [PATCH 3/6] Bind task cancel button enabled-ness to CanCancel Also update task button method names --- TwitchDownloaderWPF/PageQueue.xaml | 6 +++--- TwitchDownloaderWPF/PageQueue.xaml.cs | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/TwitchDownloaderWPF/PageQueue.xaml b/TwitchDownloaderWPF/PageQueue.xaml index cfe0dca9..3b314f45 100644 --- a/TwitchDownloaderWPF/PageQueue.xaml +++ b/TwitchDownloaderWPF/PageQueue.xaml @@ -49,12 +49,12 @@ -