1 | unit UThreading;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Forms, Contnrs, SyncObjs;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TExceptionEvent = procedure (Sender: TObject; E: Exception) of object;
|
---|
12 | TMethodCall = procedure of object;
|
---|
13 |
|
---|
14 |
|
---|
15 | { TVirtualThread }
|
---|
16 |
|
---|
17 | TVirtualThread = class
|
---|
18 | private
|
---|
19 | function GetFinished: Boolean; virtual; abstract;
|
---|
20 | function GetFreeOnTerminate: Boolean; virtual; abstract;
|
---|
21 | function GetPriority: TThreadPriority; virtual; abstract;
|
---|
22 | function GetSuspended: Boolean; virtual; abstract;
|
---|
23 | function GetTerminated: Boolean; virtual; abstract;
|
---|
24 | function GetThreadId: Integer; virtual; abstract;
|
---|
25 | procedure SetFreeOnTerminate(const AValue: Boolean); virtual; abstract;
|
---|
26 | procedure SetPriority(const AValue: TThreadPriority); virtual; abstract;
|
---|
27 | procedure SetSuspended(const AValue: Boolean); virtual; abstract;
|
---|
28 | procedure SetTerminated(const AValue: Boolean); virtual; abstract;
|
---|
29 | public
|
---|
30 | Name: string;
|
---|
31 | procedure Execute; virtual; abstract;
|
---|
32 | procedure Start; virtual; abstract;
|
---|
33 | procedure Terminate; virtual; abstract;
|
---|
34 | procedure Synchronize(AMethod: TThreadMethod); virtual; abstract;
|
---|
35 | procedure WaitFor; virtual; abstract;
|
---|
36 | procedure Sleep(Delay: Integer); virtual; abstract;
|
---|
37 | property FreeOnTerminate: Boolean read GetFreeOnTerminate
|
---|
38 | write SetFreeOnTerminate;
|
---|
39 | property Suspended: Boolean read GetSuspended
|
---|
40 | write SetSuspended;
|
---|
41 | property Priority: TThreadPriority read GetPriority write SetPriority;
|
---|
42 | property Terminated: Boolean read GetTerminated write SetTerminated;
|
---|
43 | property Finished: Boolean read GetFinished;
|
---|
44 | property ThreadId: Integer read GetThreadId;
|
---|
45 | end;
|
---|
46 |
|
---|
47 | TVirtualThreadClass = class of TVirtualThread;
|
---|
48 |
|
---|
49 | TListedThread = class;
|
---|
50 |
|
---|
51 | { TListedThreadExecute }
|
---|
52 |
|
---|
53 | TListedThreadExecute = class(TThread)
|
---|
54 | Parent: TListedThread;
|
---|
55 | procedure Execute; override;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | { TListedThread }
|
---|
59 |
|
---|
60 | TListedThread = class(TVirtualThread)
|
---|
61 | private
|
---|
62 | FTerminated: Boolean;
|
---|
63 | FFinished: Boolean;
|
---|
64 | FThread: TListedThreadExecute;
|
---|
65 | function GetFinished: Boolean; override;
|
---|
66 | function GetFreeOnTerminate: Boolean; override;
|
---|
67 | function GetPriority: TThreadPriority; override;
|
---|
68 | function GetSuspended: Boolean; override;
|
---|
69 | function GetTerminated: Boolean; override;
|
---|
70 | function GetThreadId: Integer; override;
|
---|
71 | procedure SetFreeOnTerminate(const AValue: Boolean); override;
|
---|
72 | procedure SetPriority(const AValue: TThreadPriority); override;
|
---|
73 | procedure SetSuspended(const AValue: Boolean); override;
|
---|
74 | procedure SetTerminated(const AValue: Boolean); override;
|
---|
75 | public
|
---|
76 | constructor Create(CreateSuspended: Boolean;
|
---|
77 | const StackSize: SizeUInt = DefaultStackSize);
|
---|
78 | destructor Destroy; override;
|
---|
79 | procedure Sleep(Delay: Integer); override;
|
---|
80 | procedure Execute; override;
|
---|
81 | procedure Start; override;
|
---|
82 | procedure Terminate; override;
|
---|
83 | procedure Synchronize(AMethod: TThreadMethod); override;
|
---|
84 | procedure WaitFor; override;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | TTermThreadState = (ttsReady, ttsRunning, ttsFinished, ttsExceptionOccured);
|
---|
88 |
|
---|
89 | { TTermThread }
|
---|
90 |
|
---|
91 | TTermThread = class(TListedThread)
|
---|
92 | private
|
---|
93 | FOnFinished: TNotifyEvent;
|
---|
94 | public
|
---|
95 | State: TTermThreadState;
|
---|
96 | ExceptionMessage: string;
|
---|
97 | Method: TMethodCall;
|
---|
98 | procedure Execute; override;
|
---|
99 | property OnFinished: TNotifyEvent read FOnFinished write FOnFinished;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | { TThreadList }
|
---|
103 |
|
---|
104 | TThreadList = class(TObjectList)
|
---|
105 | function FindById(Id: Integer): TVirtualThread;
|
---|
106 | constructor Create; virtual;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | var
|
---|
110 | ThreadList: TThreadList;
|
---|
111 | ThreadListLock: TCriticalSection;
|
---|
112 | OnException: TExceptionEvent;
|
---|
113 |
|
---|
114 | procedure RunInThread(Method: TMethodCall);
|
---|
115 | procedure RunInThreadAsync(Method: TMethodCall; Callback: TNotifyEvent = nil);
|
---|
116 | procedure Synchronize(Method: TMethodCall);
|
---|
117 |
|
---|
118 | resourcestring
|
---|
119 | SCurrentThreadNotFound = 'Current thread ID %d not found in virtual thread list.';
|
---|
120 |
|
---|
121 |
|
---|
122 | implementation
|
---|
123 |
|
---|
124 | procedure RunInThread(Method: TMethodCall);
|
---|
125 | var
|
---|
126 | Thread: TTermThread;
|
---|
127 | begin
|
---|
128 | try
|
---|
129 | Thread := TTermThread.Create(True);
|
---|
130 | Thread.FreeOnTerminate := False;
|
---|
131 | Thread.Method := Method;
|
---|
132 | Thread.Start;
|
---|
133 | while (Thread.State = ttsRunning) or (Thread.State = ttsReady) do begin
|
---|
134 | if MainThreadID = ThreadID then Application.ProcessMessages;
|
---|
135 | Sleep(1);
|
---|
136 | end;
|
---|
137 | if Thread.State = ttsExceptionOccured then
|
---|
138 | raise Exception.Create(Thread.ExceptionMessage);
|
---|
139 | finally
|
---|
140 | Thread.Free;
|
---|
141 | end;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure RunInThreadAsync(Method: TMethodCall; Callback: TNotifyEvent = nil);
|
---|
145 | var
|
---|
146 | Thread: TTermThread;
|
---|
147 | begin
|
---|
148 | try
|
---|
149 | Thread := TTermThread.Create(True);
|
---|
150 | Thread.FreeOnTerminate := True;
|
---|
151 | Thread.Method := Method;
|
---|
152 | Thread.OnFinished := CallBack;
|
---|
153 | Thread.Start;
|
---|
154 | //if Thread.State = ttsExceptionOccured then
|
---|
155 | // raise Exception.Create(Thread.ExceptionMessage);
|
---|
156 | finally
|
---|
157 | end;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure Synchronize(Method: TMethodCall);
|
---|
161 | var
|
---|
162 | Thread: TVirtualThread;
|
---|
163 | begin
|
---|
164 | if MainThreadID = ThreadID then Method
|
---|
165 | else begin
|
---|
166 | Thread := ThreadList.FindById(ThreadID);
|
---|
167 | if Assigned(Thread) then begin
|
---|
168 | Thread.Synchronize(Method);
|
---|
169 | end else raise Exception.Create(Format(SCurrentThreadNotFound, [ThreadID]));
|
---|
170 | end;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | { TThreadList }
|
---|
174 |
|
---|
175 | function TThreadList.FindById(Id: Integer): TVirtualThread;
|
---|
176 | var
|
---|
177 | I: Integer;
|
---|
178 | begin
|
---|
179 | I := 0;
|
---|
180 | while (I < ThreadList.Count) and (TVirtualThread(ThreadList[I]).ThreadID <> Id) do
|
---|
181 | Inc(I);
|
---|
182 | if I < ThreadList.Count then Result := TVirtualThread(ThreadList[I])
|
---|
183 | else Result := nil;
|
---|
184 | end;
|
---|
185 |
|
---|
186 | constructor TThreadList.Create;
|
---|
187 | begin
|
---|
188 | inherited Create;
|
---|
189 | end;
|
---|
190 |
|
---|
191 | { TListedThreadExecute }
|
---|
192 |
|
---|
193 | procedure TListedThreadExecute.Execute;
|
---|
194 | begin
|
---|
195 | try
|
---|
196 | try
|
---|
197 | Parent.Execute;
|
---|
198 | except
|
---|
199 | on E: Exception do
|
---|
200 | if Assigned(OnException) then
|
---|
201 | OnException(Parent.FThread, E);
|
---|
202 | end;
|
---|
203 | finally
|
---|
204 | Parent.FFinished := True;
|
---|
205 | end;
|
---|
206 | end;
|
---|
207 |
|
---|
208 | { TListedThread }
|
---|
209 |
|
---|
210 | function TListedThread.GetFinished: Boolean;
|
---|
211 | begin
|
---|
212 | Result := FFinished;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | function TListedThread.GetFreeOnTerminate: Boolean;
|
---|
216 | begin
|
---|
217 | Result := FThread.FreeOnTerminate;
|
---|
218 | end;
|
---|
219 |
|
---|
220 | function TListedThread.GetPriority: TThreadPriority;
|
---|
221 | begin
|
---|
222 | Result := FThread.Priority;
|
---|
223 | end;
|
---|
224 |
|
---|
225 | function TListedThread.GetSuspended: Boolean;
|
---|
226 | begin
|
---|
227 | Result := FThread.Suspended;
|
---|
228 | end;
|
---|
229 |
|
---|
230 | function TListedThread.GetTerminated: Boolean;
|
---|
231 | begin
|
---|
232 | Result := FTerminated;
|
---|
233 | end;
|
---|
234 |
|
---|
235 | function TListedThread.GetThreadId: Integer;
|
---|
236 | begin
|
---|
237 | Result := FThread.ThreadID;
|
---|
238 | end;
|
---|
239 |
|
---|
240 | procedure TListedThread.SetFreeOnTerminate(const AValue: Boolean);
|
---|
241 | begin
|
---|
242 | FThread.FreeOnTerminate := AValue;
|
---|
243 | end;
|
---|
244 |
|
---|
245 | procedure TListedThread.SetPriority(const AValue: TThreadPriority);
|
---|
246 | begin
|
---|
247 | FThread.Priority := AValue;
|
---|
248 | end;
|
---|
249 |
|
---|
250 | procedure TListedThread.SetSuspended(const AValue: Boolean);
|
---|
251 | begin
|
---|
252 | FThread.Suspended := AValue;
|
---|
253 | end;
|
---|
254 |
|
---|
255 | procedure TListedThread.SetTerminated(const AValue: Boolean);
|
---|
256 | begin
|
---|
257 | FTerminated := AValue;
|
---|
258 | if AValue then FThread.Terminate;
|
---|
259 | end;
|
---|
260 |
|
---|
261 | constructor TListedThread.Create(CreateSuspended: Boolean;
|
---|
262 | const StackSize: SizeUInt);
|
---|
263 | begin
|
---|
264 | FFinished := False;
|
---|
265 | FTerminated := False;
|
---|
266 |
|
---|
267 | FThread := TListedThreadExecute.Create(True, StackSize);
|
---|
268 | FThread.Parent := Self;
|
---|
269 | try
|
---|
270 | ThreadListLock.Acquire;
|
---|
271 | ThreadList.Add(Self);
|
---|
272 | finally
|
---|
273 | ThreadListLock.Release;
|
---|
274 | end;
|
---|
275 | if CreateSuspended = False then FThread.Start;
|
---|
276 | end;
|
---|
277 |
|
---|
278 | destructor TListedThread.Destroy;
|
---|
279 | begin
|
---|
280 | if not Suspended then
|
---|
281 | begin
|
---|
282 | Terminate;
|
---|
283 | WaitFor;
|
---|
284 | end;
|
---|
285 | try
|
---|
286 | ThreadListLock.Acquire;
|
---|
287 | ThreadList.Delete(ThreadList.IndexOf(Self));
|
---|
288 | finally
|
---|
289 | ThreadListLock.Release;
|
---|
290 | end;
|
---|
291 | FThread.Free;
|
---|
292 | inherited Destroy;
|
---|
293 | end;
|
---|
294 |
|
---|
295 | procedure TListedThread.Sleep(Delay: Integer);
|
---|
296 | const
|
---|
297 | Quantum = 20;
|
---|
298 | var
|
---|
299 | I: Integer;
|
---|
300 | begin
|
---|
301 | if Terminated then Exit;
|
---|
302 | SysUtils.Sleep(Delay mod Quantum);
|
---|
303 | for I := 1 to (Delay div Quantum) do begin
|
---|
304 | if Terminated then Break;
|
---|
305 | SysUtils.Sleep(Quantum);
|
---|
306 | end;
|
---|
307 | end;
|
---|
308 |
|
---|
309 | procedure TListedThread.Execute;
|
---|
310 | begin
|
---|
311 | end;
|
---|
312 |
|
---|
313 | procedure TListedThread.Start;
|
---|
314 | begin
|
---|
315 | FThread.Start;
|
---|
316 | end;
|
---|
317 |
|
---|
318 | procedure TListedThread.Terminate;
|
---|
319 | begin
|
---|
320 | FTerminated := True;
|
---|
321 | FThread.Terminate;
|
---|
322 | end;
|
---|
323 |
|
---|
324 | procedure TListedThread.Synchronize(AMethod: TThreadMethod);
|
---|
325 | begin
|
---|
326 | FThread.Synchronize(FThread, AMethod);
|
---|
327 | end;
|
---|
328 |
|
---|
329 | procedure TListedThread.WaitFor;
|
---|
330 | begin
|
---|
331 | FThread.WaitFor;
|
---|
332 | end;
|
---|
333 |
|
---|
334 | { TTermThread }
|
---|
335 |
|
---|
336 | procedure TTermThread.Execute;
|
---|
337 | begin
|
---|
338 | try
|
---|
339 | State := ttsRunning;
|
---|
340 | Method;
|
---|
341 | State := ttsFinished;
|
---|
342 | if Assigned(FOnFinished) then
|
---|
343 | FOnFinished(Self);
|
---|
344 | except
|
---|
345 | on E: Exception do begin
|
---|
346 | ExceptionMessage := E.Message;
|
---|
347 | State := ttsExceptionOccured;
|
---|
348 | if Assigned(OnException) then
|
---|
349 | OnException(FThread, E);
|
---|
350 | end;
|
---|
351 | end;
|
---|
352 | end;
|
---|
353 |
|
---|
354 | initialization
|
---|
355 |
|
---|
356 | ThreadListLock := TCriticalSection.Create;
|
---|
357 | ThreadList := TThreadList.Create;
|
---|
358 | ThreadList.OwnsObjects := False;
|
---|
359 |
|
---|
360 | finalization
|
---|
361 |
|
---|
362 | ThreadList.Free;
|
---|
363 | ThreadListLock.Free;
|
---|
364 |
|
---|
365 | end.
|
---|
366 |
|
---|