Changeset 298
- Timestamp:
- Nov 16, 2011, 10:07:51 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/UResetableThread.pas
r295 r298 23 23 private 24 24 FLock: TCriticalSection; 25 FOnException: TExceptionEvent; 25 26 FOnFinished: TNotifyEvent; 26 27 FStartEvent: TEvent; … … 42 43 property StopPending: Boolean read FStopPending; 43 44 property Running: Boolean read GetRunning; 45 property OnException: TExceptionEvent read FOnException 46 write FOnException; 44 47 end; 45 48 … … 48 51 TThreadPool = class(TThreadedPool) 49 52 private 53 FOnException: TExceptionEvent; 50 54 procedure MethodFinish(Sender: TObject); 55 procedure ThreadException(Sender: TObject; E: Exception); 51 56 protected 52 57 function NewItemObject: TObject; override; 53 58 public 59 LastExceptionClass: TClass; 60 LastExceptionMessage: string; 61 procedure CheckException; 54 62 procedure WaitForEmpty; 63 procedure Clear; 55 64 procedure RunInThread(AMethod: TMethodCall); 56 65 constructor Create; override; 57 66 destructor Destroy; override; 67 property OnException: TExceptionEvent read FOnException 68 write FOnException; 58 69 end; 59 70 … … 193 204 try 194 205 FLock.Release; 195 Method; 196 if Assigned(FOnFinished) then 197 FOnFinished(Parent); 206 try 207 try 208 Method; 209 finally 210 if Assigned(FOnFinished) then 211 FOnFinished(Parent); 212 end; 213 except 214 on E: Exception do 215 if Assigned(FOnException) then 216 FOnException(Self, E); 217 end; 198 218 finally 199 219 FLock.Acquire; … … 219 239 end; 220 240 241 procedure TThreadPool.ThreadException(Sender: TObject; E: Exception); 242 begin 243 LastExceptionClass := E.ClassType; 244 LastExceptionMessage := E.Message; 245 end; 246 247 procedure TThreadPool.CheckException; 248 begin 249 if Assigned(LastExceptionClass) then 250 raise Exception.Create(LastExceptionMessage); 251 end; 252 221 253 function TThreadPool.NewItemObject: TObject; 222 254 begin 223 255 Result := TResetableThread.Create; 256 TResetableThread(Result).OnException := ThreadException; 224 257 end; 225 258 226 259 procedure TThreadPool.WaitForEmpty; 227 260 begin 228 while UsedCount > 0 do 261 while UsedCount > 0 do begin 229 262 Sleep(1); 263 end; 264 end; 265 266 procedure TThreadPool.Clear; 267 begin 268 TotalCount := 0; 269 LastExceptionClass := nil; 270 LastExceptionMessage := ''; 230 271 end; 231 272 232 273 procedure TThreadPool.RunInThread(AMethod: TMethodCall); 233 274 begin 275 CheckException; 234 276 with TResetableThread(Acquire) do begin 235 277 Method := AMethod;
Note:
See TracChangeset
for help on using the changeset viewer.