diff --git a/TwitchDownloaderCore/Tools/VideoDownloadThread.cs b/TwitchDownloaderCore/Tools/VideoDownloadThread.cs index 1546aa42..0b672cea 100644 --- a/TwitchDownloaderCore/Tools/VideoDownloadThread.cs +++ b/TwitchDownloaderCore/Tools/VideoDownloadThread.cs @@ -51,8 +51,7 @@ public void StartDownload() private void Execute() { - using var cts = new CancellationTokenSource(); - _cancellationToken.Register(PropagateCancel, cts); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken); while (!_videoPartsQueue.IsEmpty) { @@ -81,15 +80,6 @@ private void Execute() } } - private static void PropagateCancel(object tokenSourceToCancel) - { - try - { - (tokenSourceToCancel as CancellationTokenSource)?.Cancel(); - } - catch (ObjectDisposedException) { } - } - /// The may be canceled by this method. private async Task DownloadVideoPartAsync(string videoPartName, CancellationTokenSource cancellationTokenSource) { diff --git a/TwitchDownloaderWPF/PageChatDownload.xaml.cs b/TwitchDownloaderWPF/PageChatDownload.xaml.cs index d654a034..d0e244fb 100644 --- a/TwitchDownloaderWPF/PageChatDownload.xaml.cs +++ b/TwitchDownloaderWPF/PageChatDownload.xaml.cs @@ -574,7 +574,6 @@ private async void SplitBtnDownload_Click(object sender, RoutedEventArgs e) _cancellationTokenSource.Dispose(); UpdateActionButtons(false); - currentDownload = null; GC.Collect(); } catch (Exception ex) diff --git a/TwitchDownloaderWPF/PageChatRender.xaml.cs b/TwitchDownloaderWPF/PageChatRender.xaml.cs index 8c9499d1..3beb9adb 100644 --- a/TwitchDownloaderWPF/PageChatRender.xaml.cs +++ b/TwitchDownloaderWPF/PageChatRender.xaml.cs @@ -684,7 +684,7 @@ private async void SplitBtnRender_Click(object sender, RoutedEventArgs e) UpdateActionButtons(false); currentRender.Dispose(); - GC.Collect(2, GCCollectionMode.Default, false); + GC.Collect(); } } diff --git a/TwitchDownloaderWPF/PageChatUpdate.xaml.cs b/TwitchDownloaderWPF/PageChatUpdate.xaml.cs index edd3a7a4..19fcecc3 100644 --- a/TwitchDownloaderWPF/PageChatUpdate.xaml.cs +++ b/TwitchDownloaderWPF/PageChatUpdate.xaml.cs @@ -587,7 +587,6 @@ private async void SplitBtnUpdate_Click(object sender, RoutedEventArgs e) _cancellationTokenSource.Dispose(); UpdateActionButtons(false); - currentUpdate = null; GC.Collect(); } catch (Exception ex) diff --git a/TwitchDownloaderWPF/PageVodDownload.xaml.cs b/TwitchDownloaderWPF/PageVodDownload.xaml.cs index 816b0f8a..a2ba8a68 100644 --- a/TwitchDownloaderWPF/PageVodDownload.xaml.cs +++ b/TwitchDownloaderWPF/PageVodDownload.xaml.cs @@ -471,7 +471,6 @@ private async void SplitBtnDownloader_Click(object sender, RoutedEventArgs e) _cancellationTokenSource.Dispose(); UpdateActionButtons(false); - currentDownload = null; GC.Collect(); } diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs index 621289ef..ecec89c3 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs @@ -44,7 +44,7 @@ public string StatusImage public ChatDownloadOptions DownloadOptions { get; init; } public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; set; } + public ITwitchTask DependantTask { get; init; } public string TaskType { get; } = Translations.Strings.ChatDownload; private Exception _exception; @@ -158,10 +158,8 @@ public async Task RunAsync() Exception = ex; CanReinitialize = true; } - downloader = null; TokenSource.Dispose(); - GC.Collect(); - GC.WaitForPendingFinalizers(); + GC.Collect(-1, GCCollectionMode.Default, false); } private void OnPropertyChanged([CallerMemberName] string propertyName = null) diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs index f6f1889d..13db3a4c 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs @@ -44,7 +44,7 @@ public string StatusImage public ChatRenderOptions DownloadOptions { get; init; } public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; set; } + public ITwitchTask DependantTask { get; init; } public string TaskType { get; } = Translations.Strings.ChatRender; private Exception _exception; @@ -181,7 +181,7 @@ public async Task RunAsync() } renderer.Dispose(); TokenSource.Dispose(); - GC.Collect(2, GCCollectionMode.Default, false); + GC.Collect(-1, GCCollectionMode.Default, false); } private void OnPropertyChanged([CallerMemberName] string propertyName = null) diff --git a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs index 70a9a0d4..d5072417 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs @@ -44,7 +44,7 @@ public string StatusImage public ChatUpdateOptions UpdateOptions { get; init; } public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; set; } + public ITwitchTask DependantTask { get; init; } public string TaskType { get; } = Translations.Strings.ChatUpdate; private Exception _exception; @@ -159,10 +159,8 @@ public async Task RunAsync() Exception = ex; CanReinitialize = true; } - updater = null; TokenSource.Dispose(); - GC.Collect(); - GC.WaitForPendingFinalizers(); + GC.Collect(-1, GCCollectionMode.Default, false); } private void OnPropertyChanged([CallerMemberName] string propertyName = null) diff --git a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs index 3d8dc08d..c130bbe9 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs @@ -44,7 +44,7 @@ public string StatusImage public ClipDownloadOptions DownloadOptions { get; init; } public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; set; } + public ITwitchTask DependantTask { get; init; } public string TaskType { get; } = Translations.Strings.ClipDownload; private Exception _exception; @@ -158,10 +158,8 @@ public async Task RunAsync() Exception = ex; CanReinitialize = true; } - downloader = null; TokenSource.Dispose(); - GC.Collect(); - GC.WaitForPendingFinalizers(); + GC.Collect(-1, GCCollectionMode.Default, false); } private void OnPropertyChanged([CallerMemberName] string propertyName = null) diff --git a/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs b/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs index 36f496ae..349e69ff 100644 --- a/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs @@ -24,7 +24,7 @@ public interface ITwitchTask : INotifyPropertyChanged string DisplayStatus { get; } string StatusImage { get; } CancellationTokenSource TokenSource { get; } - ITwitchTask DependantTask { get; set; } + ITwitchTask DependantTask { get; init; } string TaskType { get; } Exception Exception { get; } string OutputFile { get; } diff --git a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs index fe885717..e80bc5a2 100644 --- a/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs +++ b/TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs @@ -44,7 +44,7 @@ public string StatusImage public VideoDownloadOptions DownloadOptions { get; init; } public CancellationTokenSource TokenSource { get; private set; } = new(); - public ITwitchTask DependantTask { get; set; } + public ITwitchTask DependantTask { get; init; } public string TaskType { get; } = Translations.Strings.VodDownload; private Exception _exception; @@ -158,10 +158,8 @@ public async Task RunAsync() Exception = ex; CanReinitialize = true; } - downloader = null; TokenSource.Dispose(); - GC.Collect(); - GC.WaitForPendingFinalizers(); + GC.Collect(-1, GCCollectionMode.Default, false); } private void OnPropertyChanged([CallerMemberName] string propertyName = null) diff --git a/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs b/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs index cd9c0259..33f3e0c2 100644 --- a/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs +++ b/TwitchDownloaderWPF/WindowQueueOptions.xaml.cs @@ -206,10 +206,10 @@ private void btnQueue_Click(object sender, RoutedEventArgs e) { Title = vodDownloadPage.textTitle.Text, Thumbnail = vodDownloadPage.imgThumbnail.Source - } + }, + DependantTask = chatTask, }; renderTask.ChangeStatus(TwitchTaskStatus.Waiting); - renderTask.DependantTask = chatTask; lock (PageQueue.taskLock) {