Skip to content

Commit

Permalink
Cleanup (#1196)
Browse files Browse the repository at this point in the history
* Use CancellationTokenSource.CreateLinkedTokenSource

* Remove redundant code

* Non-blocking GC

* Make ITwitchTask.DependantTask init
  • Loading branch information
ScrubN authored Aug 27, 2024
1 parent 818aa28 commit d54ae22
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 36 deletions.
12 changes: 1 addition & 11 deletions TwitchDownloaderCore/Tools/VideoDownloadThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -81,15 +80,6 @@ private void Execute()
}
}

private static void PropagateCancel(object tokenSourceToCancel)
{
try
{
(tokenSourceToCancel as CancellationTokenSource)?.Cancel();
}
catch (ObjectDisposedException) { }
}

/// <remarks>The <paramref name="cancellationTokenSource"/> may be canceled by this method.</remarks>
private async Task DownloadVideoPartAsync(string videoPartName, CancellationTokenSource cancellationTokenSource)
{
Expand Down
1 change: 0 additions & 1 deletion TwitchDownloaderWPF/PageChatDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,6 @@ private async void SplitBtnDownload_Click(object sender, RoutedEventArgs e)
_cancellationTokenSource.Dispose();
UpdateActionButtons(false);

currentDownload = null;
GC.Collect();
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/PageChatRender.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
1 change: 0 additions & 1 deletion TwitchDownloaderWPF/PageChatUpdate.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ private async void SplitBtnUpdate_Click(object sender, RoutedEventArgs e)
_cancellationTokenSource.Dispose();
UpdateActionButtons(false);

currentUpdate = null;
GC.Collect();
}
catch (Exception ex)
Expand Down
1 change: 0 additions & 1 deletion TwitchDownloaderWPF/PageVodDownload.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ private async void SplitBtnDownloader_Click(object sender, RoutedEventArgs e)
_cancellationTokenSource.Dispose();
UpdateActionButtons(false);

currentDownload = null;
GC.Collect();
}

Expand Down
6 changes: 2 additions & 4 deletions TwitchDownloaderWPF/TwitchTasks/ChatDownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions TwitchDownloaderWPF/TwitchTasks/ChatRenderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions TwitchDownloaderWPF/TwitchTasks/ChatUpdateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions TwitchDownloaderWPF/TwitchTasks/ClipDownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderWPF/TwitchTasks/ITwitchTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
6 changes: 2 additions & 4 deletions TwitchDownloaderWPF/TwitchTasks/VodDownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions TwitchDownloaderWPF/WindowQueueOptions.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit d54ae22

Please sign in to comment.