From 174cf226af3f04afa0c8d30b44d15bafe004693d Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sat, 3 Aug 2024 17:35:29 -0400 Subject: [PATCH] Fix race condition --- TwitchDownloaderWPF/PageQueue.xaml.cs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) 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; } }