87 lines
2.6 KiB
C#
Raw Normal View History

2022-06-27 13:30:35 +08:00
using System;
using System.Threading;
using System.Collections.Concurrent;
namespace RageCoop.Core
{
2022-07-01 14:39:43 +08:00
/// <summary>
/// A worker that constantly execute jobs in a background thread.
/// </summary>
2022-06-27 13:30:35 +08:00
public class Worker:IDisposable
{
private SemaphoreSlim _semaphoreSlim;
private Thread _workerThread;
private bool _stopping=false;
2022-07-01 14:39:43 +08:00
/// <summary>
/// Name of the worker
/// </summary>
2022-06-27 13:30:35 +08:00
public string Name { get; set; }
2022-07-01 14:39:43 +08:00
/// <summary>
/// Whether this worker is busy executing job(s).
/// </summary>
2022-06-27 13:30:35 +08:00
public bool IsBusy { get;private set; }
2022-06-30 09:28:13 +08:00
internal Worker(string name,Logger logger,int maxJobs = Int32.MaxValue)
2022-06-27 13:30:35 +08:00
{
Name = name;
2022-06-30 09:28:13 +08:00
_semaphoreSlim = new SemaphoreSlim(0,maxJobs);
2022-06-27 13:30:35 +08:00
_workerThread=new Thread(() =>
{
while (!_stopping)
{
IsBusy=false;
_semaphoreSlim.Wait();
if(Jobs.TryDequeue(out var job))
{
IsBusy=true;
2022-06-30 09:28:13 +08:00
try
{
job.Invoke();
}
catch (Exception ex)
{
logger.Error("Error occurred when executing queued job:");
logger.Error(ex);
}
2022-06-27 13:30:35 +08:00
}
else
{
throw new InvalidOperationException("Hmm... that's unexpected.");
}
}
IsBusy=false;
});
_workerThread.Start();
}
2022-07-01 14:39:43 +08:00
/// <summary>
/// Queue a job to be executed
/// </summary>
/// <param name="work"></param>
2022-06-30 09:28:13 +08:00
public void QueueJob(Action work)
2022-06-27 13:30:35 +08:00
{
Jobs.Enqueue(work);
_semaphoreSlim.Release();
}
2022-07-01 14:39:43 +08:00
/// <summary>
/// Finish current job and stop the worker.
/// </summary>
2022-06-27 13:30:35 +08:00
public void Stop()
{
_stopping=true;
2022-06-30 09:28:13 +08:00
QueueJob(() => { });
2022-06-27 13:30:35 +08:00
if (_workerThread.IsAlive)
{
_workerThread.Join();
}
}
2022-07-01 14:39:43 +08:00
/// <summary>
2022-07-01 17:00:42 +08:00
/// Finish current job and stop the worker.
2022-07-01 14:39:43 +08:00
/// </summary>
2022-06-27 13:30:35 +08:00
public void Dispose()
{
Stop();
_semaphoreSlim.Dispose();
}
private ConcurrentQueue<Action> Jobs=new ConcurrentQueue<Action>();
}
}