fix BackgroundWorker single thread

This commit is contained in:
ngfrolov 2023-11-29 09:02:26 +05:00
parent 409e1be983
commit 15d329999b
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 7 additions and 2 deletions

View File

@ -31,6 +31,7 @@ public class BackgroundWorker : BackgroundService
/// Работа выполняемая в данный момент
/// </summary>
public Work? CurrentWork;
private bool isRuning;
/// <summary>
/// последние 16 завершившиеся с ошибкой
@ -54,6 +55,9 @@ public class BackgroundWorker : BackgroundService
protected override async Task ExecuteAsync(CancellationToken token)
{
if (isRuning)
return;
isRuning = true;
Trace.TraceInformation($"{GetType().Name} started");
while (!token.IsCancellationRequested && works.TryDequeue(out CurrentWork))
{
@ -82,6 +86,7 @@ public class BackgroundWorker : BackgroundService
Trace.TraceError(MainLoopLastException);
}
}
isRuning = false;
}
/// <summary>
@ -96,7 +101,7 @@ public class BackgroundWorker : BackgroundService
{
works.Enqueue(work);
if (ExecuteTask is null || ExecuteTask.IsCompleted)
StartAsync(CancellationToken.None).Wait();
StartAsync(CancellationToken.None);
}
/// <summary>

View File

@ -103,7 +103,7 @@ public class PeriodicBackgroundWorker : BackgroundService
var periodic = new WorkPeriodic(work, period);
works.Add(periodic);
if (ExecuteTask is null || ExecuteTask.IsCompleted)
StartAsync(CancellationToken.None).Wait();
StartAsync(CancellationToken.None);
}
private WorkPeriodic? GetNext()