Skip to content

Commit

Permalink
Fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Aug 3, 2024
1 parent 23d67e0 commit 174cf22
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions TwitchDownloaderWPF/PageQueue.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ namespace TwitchDownloaderWPF
public partial class PageQueue : Page
{
public static readonly object taskLock = new object();
public static ObservableCollection<ITwitchTask> taskList { get; set; } = new ObservableCollection<ITwitchTask>();
BackgroundWorker taskManager = new BackgroundWorker();
public static ObservableCollection<ITwitchTask> taskList { get; } = new ObservableCollection<ITwitchTask>();
private static readonly BackgroundWorker taskManager = new BackgroundWorker();

public PageQueue()
{
Expand All @@ -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)
{
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -99,7 +102,6 @@ private void TaskManager_DoWork(object sender, DoWorkEventArgs e)
task.RunAsync();
break;
}
continue;
}
}

Expand Down

0 comments on commit 174cf22

Please sign in to comment.