diff --git a/TwitchDownloaderWPF/PageQueue.xaml.cs b/TwitchDownloaderWPF/PageQueue.xaml.cs index 4ab7508d..1343b640 100644 --- a/TwitchDownloaderWPF/PageQueue.xaml.cs +++ b/TwitchDownloaderWPF/PageQueue.xaml.cs @@ -17,8 +17,8 @@ namespace TwitchDownloaderWPF public partial class PageQueue : Page { public static readonly object taskLock = new object(); - public static ObservableCollection taskList { get; set; } = new ObservableCollection(); - BackgroundWorker taskManager = new BackgroundWorker(); + public static ObservableCollection taskList { get; } = new ObservableCollection(); + private static readonly BackgroundWorker taskManager = new BackgroundWorker(); public PageQueue() { @@ -34,7 +34,7 @@ public PageQueue() taskManager.RunWorkerAsync(); } - private void TaskManager_DoWork(object sender, DoWorkEventArgs e) + private static void TaskManager_DoWork(object sender, DoWorkEventArgs e) { while (true) { @@ -47,10 +47,13 @@ private void TaskManager_DoWork(object sender, DoWorkEventArgs e) int currentChat = 0; int currentRender = 0; - foreach (var task in taskList) + lock (taskLock) { - if (task.Status == TwitchTaskStatus.Running) + foreach (var task in taskList) { + if (task.Status is not TwitchTaskStatus.Running) + continue; + switch (task) { case VodDownloadTask: @@ -70,12 +73,12 @@ private void TaskManager_DoWork(object sender, DoWorkEventArgs e) break; } } - } - foreach (var task in taskList) - { - if (task.CanRun()) + foreach (var task in taskList) { + if (!task.CanRun()) + continue; + switch (task) { case VodDownloadTask when currentVod < maxVod: @@ -99,7 +102,6 @@ private void TaskManager_DoWork(object sender, DoWorkEventArgs e) task.RunAsync(); break; } - continue; } }