/
ThreadWrappers.cs
247 lines (239 loc) · 10.5 KB
/
ThreadWrappers.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
ThreadWrappers.cs includes a framework of classes to be used as wrappers around threads.
Instead of using the Thread class, it allows you to create a class inherited from one
of the base classes, override a method and invoke.
Copyright (C) 2012 Mohamed Abu Marsa a.k.a. (VC, kOVC) (vc@korganization.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The GNU General Public License copy can be found in the COPYRIGHT file
at the root of the project directory structure.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace CJ {
#region Callback Delegates
public delegate void ThreadProc();
public delegate void ThreadProc<ParameterType>(ParameterType arg);
public delegate void ThreadProc<Parameter1Type, Parameter2Type>(Parameter1Type arg1, Parameter2Type arg2);
public delegate ReturnType ThreadMethod<ReturnType>();
public delegate ReturnType ThreadMethod<ReturnType, ParameterType>(ParameterType arg);
public delegate ReturnType ThreadMethod<ReturnType, Parameter1Type, Parameter2Type>(Parameter1Type arg1, Parameter2Type arg2);
#endregion
#region event arguments class CancelEventArgs
public class CancelEventArgs : EventArgs {
#region Private Fields
private bool _cancel;
#endregion
#region Public Properties
public bool Cancel { get { return _cancel; } set { _cancel = value; } }
#endregion
#region Constructor
public CancelEventArgs() { _cancel = false; }
#endregion
}
#endregion
#region abstract class ThreadWrapperBase
public abstract class ThreadWrapperBase {
#region Protected Fields
protected Thread _thread;
protected bool _started;
#endregion
#region Abstract Methods
/// <summary>
/// This method should be overriden in child classes and the implementation will
/// simply be to call the actual thread parametrized method and save its return value
/// in the result field (which should be added if needed)
/// </summary>
public abstract void Run();
#endregion
#region Implementation Methods
public void Start() {
CancelEventArgs e = new CancelEventArgs(); OnThreadStarting(e);
if (!e.Cancel) { _thread.Start(); OnThreadStarted(EventArgs.Empty); _started = true; }
}
public void Abort() {
CancelEventArgs e = new CancelEventArgs(); OnThreadAborting(e);
if (!e.Cancel) { _thread.Abort(); OnThreadAborted(EventArgs.Empty); }
}
public void Join() { _thread.Join(); }
public void Join(int millisecondsTimeOut) { _thread.Join(millisecondsTimeOut); }
public void Join(TimeSpan timeout) { _thread.Join(timeout); }
public bool Started { get { return _started; } }
#endregion
#region Events
public event EventHandler<CancelEventArgs> ThreadStarting;
public event EventHandler<CancelEventArgs> ThreadAborting;
public event EventHandler ThreadStarted;
public event EventHandler ThreadAborted;
#endregion
#region Event Firing Methods
protected virtual void OnThreadStarting(CancelEventArgs args) { if (ThreadStarting != null) { ThreadStarting(this, args); } }
protected virtual void OnThreadAborting(CancelEventArgs args) { if (ThreadAborting != null) { ThreadAborting(this, args); } }
protected virtual void OnThreadStarted(EventArgs args) { if (ThreadStarted != null) { ThreadStarted(this, args); } }
protected virtual void OnThreadAborted(EventArgs args) { if (ThreadAborted != null) { ThreadAborted(this, args); } }
#endregion
#region Protected Constructor
protected ThreadWrapperBase() { _thread = new Thread(new ThreadStart(Run)); _started = false; }
#endregion
}
#endregion
#region abstract class MethodThreadWrapperBase<ReturnType>
public abstract class MethodThreadWrapperBase<ReturnType> : ThreadWrapperBase {
#region Protected Fields
protected ReturnType _return;
protected bool _finished;
#endregion
#region Properties
public ReturnType Result { get { return _return; } }
public bool ResultReady { get { return _finished; } }
#endregion
#region Events
public event EventHandler Finished;
#endregion
#region Event Firing Methods
protected virtual void OnFinished(EventArgs args) { if (Finished != null) { Finished(this, args); } }
#endregion
#region Constructor
protected MethodThreadWrapperBase() : base() { }
#endregion
}
#endregion
//Procedure (methods that return void) Thread Wrappers
#region class ProcThreadWrapper - void(void) wrapper
public class ProcThreadWrapper : ThreadWrapperBase {
#region Protected Fields
protected ThreadProc _method;
#endregion
#region Base Overrides
public override void Run() {
_method();
}
#endregion
#region Constructor(s)
protected ProcThreadWrapper() : base() { }
public ProcThreadWrapper(ThreadProc method)
: base() {
_method = method;
}
#endregion
}
#endregion
#region class ProcThreadWrapper<ParameterType> - void(ParameterType) wrappers
public class ProcThreadWrapper<ParameterType> : ThreadWrapperBase {
#region Protected Fields
protected ParameterType _parameter;
protected ThreadProc<ParameterType> _method;
#endregion
#region Base Overrides
public override void Run() {
_method(_parameter);
}
#endregion
#region Constructor(s)
protected ProcThreadWrapper() : base() { }
public ProcThreadWrapper(ThreadProc<ParameterType> method, ParameterType parameter)
: base() {
_parameter = parameter; _method = method;
}
#endregion
}
#endregion
#region class ProcThreadWrapper<Parameter1Type, Parameter2Type> - void(Parameter1Type, Parameter2Type) wrappers
public class ProcThreadWrapper<Parameter1Type, Parameter2Type> : ThreadWrapperBase {
#region Protected Fields
protected Parameter1Type _parameter1;
protected Parameter2Type _parameter2;
protected ThreadProc<Parameter1Type, Parameter2Type> _method;
#endregion
#region Base Overrides
public override void Run() {
_method(_parameter1, _parameter2);
}
#endregion
#region Constructor(s)
protected ProcThreadWrapper() : base() { }
public ProcThreadWrapper(ThreadProc<Parameter1Type, Parameter2Type> method, Parameter1Type parameter1, Parameter2Type parameter2)
: base() {
_parameter2 = parameter2; _parameter1 = parameter1; _method = method;
}
#endregion
}
#endregion
//Method Thread Wrappers
#region class MethodThreadWrapper<ReturnType> - ReturnType(void) wrappers
public class MethodThreadWrapper<ReturnType> : MethodThreadWrapperBase<ReturnType> {
#region Protected Fields
protected ThreadMethod<ReturnType> _method;
#endregion
#region Base Overrides
public override void Run() {
_return = _method();
_finished = true;
OnFinished(EventArgs.Empty);
}
#endregion
#region Constructor(s)
protected MethodThreadWrapper() : base() { _finished = false; }
public MethodThreadWrapper(ThreadMethod<ReturnType> method)
: base() {
_method = method; _finished = false;
}
#endregion
}
#endregion
#region class MethodThreadWrapper<ReturnType, ParameterType> - ReturnType(ParameterType) wrappers
public class MethodThreadWrapper<ReturnType, ParameterType> : MethodThreadWrapperBase<ReturnType> {
#region Protected Fields
protected ThreadMethod<ReturnType, ParameterType> _method;
protected ParameterType _parameter;
#endregion
#region Base Overrides
public override void Run() {
_return = _method(_parameter);
_finished = true;
OnFinished(EventArgs.Empty);
}
#endregion
#region Constructor(s)
protected MethodThreadWrapper() : base() { _finished = false; }
public MethodThreadWrapper(ThreadMethod<ReturnType, ParameterType> method, ParameterType parameter)
: base() {
_method = method; _finished = false; _parameter = parameter;
}
#endregion
}
#endregion
#region class MethodThreadWrapper<ReturnType, Parameter1Type, Parameter2Type> - ReturnType(Parameter1Type, Parameter2Type) wrappers
public class MethodThreadWrapper<ReturnType, Parameter1Type, Parameter2Type> : MethodThreadWrapperBase<ReturnType> {
#region Protected Fields
protected ThreadMethod<ReturnType, Parameter1Type, Parameter2Type> _method;
protected Parameter1Type _parameter1;
protected Parameter2Type _parameter2;
#endregion
#region Base Overrides
public override void Run() {
_return = _method(_parameter1, _parameter2);
_finished = true;
OnFinished(EventArgs.Empty);
}
#endregion
#region Constructor(s)
protected MethodThreadWrapper() : base() { _finished = false; }
public MethodThreadWrapper(ThreadMethod<ReturnType, Parameter1Type, Parameter2Type> method, Parameter1Type parameter1, Parameter2Type parameter2)
: base() {
_method = method; _finished = false; _parameter1 = parameter1; _parameter2 = parameter2;
}
#endregion
}
#endregion
}