-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseThread.cs
97 lines (76 loc) · 2.74 KB
/
BaseThread.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Threading;
using NLog;
namespace ThreadSupport
{
/// <summary>
/// This BaseThread class creates a BlockingQueue of type ThreadMessage for the given thread. The name of the queue is
/// specified on the constructor.
///
/// Additionally:
/// this class creates and instantiates a thread, using the derived classes Runner() method.
/// this class adds the name of the queue to the queuemanager singleton object
///
/// </summary>
public abstract class BaseThread
{
private volatile bool _running;
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private CancellationToken _cts;
public bool isCancellationRequested
{
get { return _cts.IsCancellationRequested; }
}
public string Qname { get; }
public bool Running
{
get { return _running; }
set { _running = value; }
}
private readonly BlockingQueue<ThreadMessage> _myQueue;
public BlockingQueue<ThreadMessage> MyQueue
{
get { return _myQueue; }
}
public string ThreadName { get; }
private Thread _thisThread;
public Thread ThisThread
{
get { return _thisThread; }
}
private static readonly QueueManager qm = QueueManager.Instance;
protected BaseThread(String threadName, String queueName, CancellationToken cts)
{
// Setup our cancellation token
_cts = cts;
// Set our name
ThreadName = threadName;
logger.Trace(ThreadName + "|Starting");
// Create our queue
_myQueue = new BlockingQueue<ThreadMessage>();
//_myQueue.SetupQueueStats(queueName);
// Name our queue statistics counter
//_myQueue.SetupQueueStats(ThreadName + "Q");
// Add it to the queuemanager so other threads can easily locate this queue
qm.AddQueue(ref _myQueue, queueName);
Qname = queueName;
// Fire up the Runner method from the derived class
ThreadStart threadStarter = Runner;
_thisThread = new Thread(threadStarter);
}
public void Start()
{
_thisThread.Start();
}
protected bool TakeFunc(ThreadMessage tm, int i)
{
//_myQueue.DecrementQueueItemCounter();
if ((tm.Cmd == Defines.ThreadExitMsg))
{
logger.Trace(ThreadName + "|Received Exit Command");
}
return (tm.Cmd != Defines.ThreadExitMsg);
}
protected abstract void Runner();
}
}