1: /// <summary>
2: /// Worker class used in multi threading
3: /// </summary>
4: /// <typeparam name="ResultType">Result type</typeparam>
5: /// <typeparam name="InputParams">The input parameter type</typeparam>
6: public abstract class Worker<ResultType,InputParams>
7: {
8: #region Constructor
9:
10: /// <summary>
11: /// Constructor
12: /// </summary>
13: /// <param name="Params">Parameters used in the function</param>
14: protected Worker(InputParams Params)
15: {
16: this.Params = Params;
17: this.WorkerThread = new Thread(DoWork);
18: }
19:
20: #endregion
21:
22: #region Protected Abstract Functions
23:
24: /// <summary>
25: /// Function that actually does the work
26: /// </summary>
27: /// <param name="Params">Parameter used by the function</param>
28: /// <returns>The result of the function</returns>
29: protected abstract ResultType Work(InputParams Params);
30:
31: #endregion
32:
33: #region Public Functions
34:
35: /// <summary>
36: /// Starts the thread
37: /// </summary>
38: public void Start()
39: {
40: this.WorkerThread.Start();
41: }
42:
43: /// <summary>
44: /// Stops the thread and waits for it to finish
45: /// </summary>
46: public void Stop()
47: {
48: if (WorkerThread != null && WorkerThread.IsAlive)
49: {
50: this.WorkerThread.Join();
51: this.WorkerThread = null;
52: }
53: OnEndEventArgs EndEvents = new OnEndEventArgs();
54: EndEvents.Content = Result;
55: EventHelper.Raise<OnEndEventArgs>(Finished, this, EndEvents);
56: }
57:
58: #endregion
59:
60: #region Private Functions
61:
62: /// <summary>
63: /// Wrapper for the function that actually does the work.
64: /// Calls the start and finished events as well as stores
65: /// the result for use within the class.
66: /// </summary>
67: private void DoWork()
68: {
69: EventHelper.Raise<OnStartEventArgs>(Started, this, new OnStartEventArgs());
70:
71: Result = Work(this.Params);
72:
73: OnEndEventArgs EndEvents = new OnEndEventArgs();
74: EndEvents.Content = Result;
75: EventHelper.Raise<OnEndEventArgs>(Finished, this, EndEvents);
76: }
77:
78: #endregion
79:
80: #region Events
81:
82: /// <summary>
83: /// Called when the thread is finished
84: /// </summary>
85: public EventHandler<OnEndEventArgs> Finished { get; set; }
86:
87: /// <summary>
88: /// Called when the thread is started
89: /// </summary>
90: public EventHandler<OnStartEventArgs> Started { get; set; }
91:
92: /// <summary>
93: /// Can be used by the worker function to indicate progress
94: /// </summary>
95: public EventHandler<ChangedEventArgs> Updated { get; set; }
96:
97: /// <summary>
98: /// Can be used by the worker function to indicate an exception has occurred
99: /// </summary>
100: public EventHandler<OnErrorEventArgs> Exception { get; set; }
101:
102: #endregion
103:
104: #region Properties
105:
106: /// <summary>
107: /// Indicates whether or not the thread is stopped/started
108: /// </summary>
109: public bool Stopped
110: {
111: get
112: {
113: if (WorkerThread != null && WorkerThread.IsAlive)
114: return false;
115: return true;
116: }
117: }
118:
119: /// <summary>
120: /// The result (can be used by the class that inherits from this base class
121: /// </summary>
122: protected ResultType Result { get; set; }
123:
124: /// <summary>
125: /// Parameters used in the function
126: /// </summary>
127: private InputParams Params = default(InputParams);
128:
129: /// <summary>
130: /// The thread used
131: /// </summary>
132: private Thread WorkerThread=null;
133:
134: #endregion
135: }