using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace Core.Util
{
public class TaskQueue
{
#region 构造函数
public TaskQueue()
{
_timeSpan = TimeSpan.Zero;
Start();
}
public TaskQueue(TimeSpan timeSpan)
{
_timeSpan = timeSpan;
Start();
}
#endregion
#region 私有成员
Semaphore _semaphore { get; } = new Semaphore(0, int.MaxValue);
private void Start()
{
Task.Factory.StartNew(() =>
{
while (_isRun)
{
try
{
_semaphore.WaitOne();
bool success = _taskList.TryDequeue(out Action task);
if (success)
{
task?.Invoke();
}
if (_timeSpan != TimeSpan.Zero)
Thread.Sleep(_timeSpan);
}
catch (Exception ex)
{
HandleException?.Invoke(ex);
}
}
}, TaskCreationOptions.LongRunning);
}
private bool _isRun { get; set; } = true;
private TimeSpan _timeSpan { get; set; }
private ConcurrentQueue<Action> _taskList { get; } = new ConcurrentQueue<Action>();
#endregion
#region 外部接口
public void Stop()
{
_isRun = false;
}
public void Enqueue(Action task)
{
_taskList.Enqueue(task);
_semaphore.Release();
}
public Action<Exception> HandleException { get; set; }
#endregion
}
}