2021-06-09 16:43:27 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2021-07-04 12:56:52 +00:00
|
|
|
|
|
2021-06-09 16:43:27 +00:00
|
|
|
|
namespace Matrix_App
|
|
|
|
|
{
|
|
|
|
|
public class ThreadQueue
|
|
|
|
|
{
|
|
|
|
|
public delegate bool Task();
|
|
|
|
|
|
|
|
|
|
private readonly Queue<Task> taskQueue = new Queue<Task>();
|
|
|
|
|
|
|
|
|
|
private readonly Thread thread;
|
|
|
|
|
|
|
|
|
|
private volatile bool running;
|
|
|
|
|
private volatile bool working;
|
|
|
|
|
|
|
|
|
|
private readonly int capacity;
|
|
|
|
|
|
|
|
|
|
public ThreadQueue(string name, int capacity)
|
|
|
|
|
{
|
|
|
|
|
this.capacity = capacity;
|
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
|
|
thread = new Thread(IdleQueue)
|
|
|
|
|
{
|
|
|
|
|
Name = name,
|
|
|
|
|
IsBackground = true,
|
2021-06-11 11:36:55 +00:00
|
|
|
|
Priority = ThreadPriority.Normal
|
2021-06-09 16:43:27 +00:00
|
|
|
|
};
|
|
|
|
|
thread.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void IdleQueue()
|
|
|
|
|
{
|
|
|
|
|
while(running)
|
|
|
|
|
{
|
2021-07-04 12:56:52 +00:00
|
|
|
|
lock (taskQueue)
|
2021-06-09 16:43:27 +00:00
|
|
|
|
{
|
2021-07-04 12:56:52 +00:00
|
|
|
|
working = taskQueue.Count > 0;
|
2021-06-09 16:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 12:56:52 +00:00
|
|
|
|
if (working)
|
2021-06-09 16:43:27 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-07-04 12:56:52 +00:00
|
|
|
|
Task task;
|
|
|
|
|
lock (taskQueue)
|
|
|
|
|
{
|
|
|
|
|
task = taskQueue.Dequeue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
running = task();
|
|
|
|
|
|
|
|
|
|
lock (taskQueue)
|
|
|
|
|
{
|
|
|
|
|
working = taskQueue.Count > 0;
|
|
|
|
|
}
|
2021-06-09 16:43:27 +00:00
|
|
|
|
} catch(ThreadInterruptedException)
|
|
|
|
|
{
|
|
|
|
|
thread.Interrupt();
|
|
|
|
|
running = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-04 12:56:52 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2021-08-08 12:50:24 +00:00
|
|
|
|
lock (taskQueue)
|
2021-07-04 12:56:52 +00:00
|
|
|
|
{
|
2021-08-08 12:50:24 +00:00
|
|
|
|
Monitor.Wait(taskQueue);
|
2021-07-04 12:56:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-09 16:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Enqueue(Task task)
|
|
|
|
|
{
|
|
|
|
|
lock (taskQueue)
|
|
|
|
|
{
|
2021-07-04 12:56:52 +00:00
|
|
|
|
if (taskQueue.Count >= capacity)
|
|
|
|
|
return;
|
2021-08-08 12:50:24 +00:00
|
|
|
|
|
|
|
|
|
Monitor.Pulse(taskQueue);
|
2021-07-04 12:56:52 +00:00
|
|
|
|
|
|
|
|
|
working = true;
|
|
|
|
|
taskQueue.Enqueue(task);
|
2021-06-09 16:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasWork()
|
|
|
|
|
{
|
|
|
|
|
return working;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
2021-08-08 12:50:24 +00:00
|
|
|
|
lock (taskQueue)
|
|
|
|
|
{
|
|
|
|
|
Monitor.Pulse(taskQueue);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 16:43:27 +00:00
|
|
|
|
running = false;
|
|
|
|
|
|
|
|
|
|
thread.Interrupt();
|
2021-07-04 12:56:52 +00:00
|
|
|
|
thread.Join(100);
|
2021-06-09 16:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|