forked from ddrilling/AsbCloudServer
fix BackgroundWorker single thread
This commit is contained in:
parent
409e1be983
commit
15d329999b
@ -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>
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user