Changeset 75 for trunk/Packages/Common/Threading.pas
- Timestamp:
- Jun 4, 2024, 12:22:49 AM (5 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/Threading.pas
r74 r75 1 unit UThreading; 2 3 {$mode delphi} 1 unit Threading; 4 2 5 3 interface 6 4 7 5 uses 8 Classes, SysUtils, Forms, Contnrs, SyncObjs;6 Classes, SysUtils, Forms, Generics.Collections, SyncObjs; 9 7 10 8 type 11 9 TExceptionEvent = procedure (Sender: TObject; E: Exception) of object; 12 10 TMethodCall = procedure of object; 13 14 11 15 12 { TVirtualThread } … … 22 19 function GetSuspended: Boolean; virtual; abstract; 23 20 function GetTerminated: Boolean; virtual; abstract; 24 function GetThreadId: Integer; virtual; abstract;21 function GetThreadId: TThreadID; virtual; abstract; 25 22 procedure SetFreeOnTerminate(const AValue: Boolean); virtual; abstract; 26 23 procedure SetPriority(const AValue: TThreadPriority); virtual; abstract; … … 30 27 Name: string; 31 28 procedure Execute; virtual; abstract; 32 procedure Resume; virtual; abstract;33 procedure Suspend; virtual; abstract;34 29 procedure Start; virtual; abstract; 35 30 procedure Terminate; virtual; abstract; … … 44 39 property Terminated: Boolean read GetTerminated write SetTerminated; 45 40 property Finished: Boolean read GetFinished; 46 property ThreadId: Integerread GetThreadId;41 property ThreadId: TThreadID read GetThreadId; 47 42 end; 48 43 … … 70 65 function GetSuspended: Boolean; override; 71 66 function GetTerminated: Boolean; override; 72 function GetThreadId: Integer; override;67 function GetThreadId: TThreadID; override; 73 68 procedure SetFreeOnTerminate(const AValue: Boolean); override; 74 69 procedure SetPriority(const AValue: TThreadPriority); override; … … 81 76 procedure Sleep(Delay: Integer); override; 82 77 procedure Execute; override; 83 procedure Resume; override;84 procedure Suspend; override;85 78 procedure Start; override; 86 79 procedure Terminate; override; … … 106 99 { TThreadList } 107 100 108 TThreadList = class(TObjectList )109 function FindById(Id: Integer): TVirtualThread;101 TThreadList = class(TObjectList<TVirtualThread>) 102 function FindById(Id: TThreadID): TVirtualThread; 110 103 constructor Create; virtual; 111 104 end; … … 134 127 Thread.FreeOnTerminate := False; 135 128 Thread.Method := Method; 136 Thread. Resume;129 Thread.Start; 137 130 while (Thread.State = ttsRunning) or (Thread.State = ttsReady) do begin 138 131 if MainThreadID = ThreadID then Application.ProcessMessages; … … 155 148 Thread.Method := Method; 156 149 Thread.OnFinished := CallBack; 157 Thread. Resume;150 Thread.Start; 158 151 //if Thread.State = ttsExceptionOccured then 159 152 // raise Exception.Create(Thread.ExceptionMessage); … … 168 161 if MainThreadID = ThreadID then Method 169 162 else begin 170 Thread := ThreadList.FindById(ThreadID); 163 try 164 ThreadListLock.Acquire; 165 Thread := ThreadList.FindById(ThreadID); 166 finally 167 ThreadListLock.Release; 168 end; 171 169 if Assigned(Thread) then begin 172 170 Thread.Synchronize(Method); … … 177 175 { TThreadList } 178 176 179 function TThreadList.FindById(Id: Integer): TVirtualThread;177 function TThreadList.FindById(Id: TThreadID): TVirtualThread; 180 178 var 181 179 I: Integer; 182 180 begin 183 181 I := 0; 184 while (I < ThreadList.Count) and (T VirtualThread(ThreadList[I]).ThreadID <> Id) do182 while (I < ThreadList.Count) and (ThreadList[I].ThreadID <> Id) do 185 183 Inc(I); 186 if I < ThreadList.Count then Result := T VirtualThread(ThreadList[I])184 if I < ThreadList.Count then Result := ThreadList[I] 187 185 else Result := nil; 188 186 end; … … 237 235 end; 238 236 239 function TListedThread.GetThreadId: Integer;237 function TListedThread.GetThreadId: TThreadID; 240 238 begin 241 239 Result := FThread.ThreadID; … … 294 292 end; 295 293 FThread.Free; 296 inherited Destroy;294 inherited; 297 295 end; 298 296 … … 313 311 procedure TListedThread.Execute; 314 312 begin 315 end;316 317 procedure TListedThread.Resume;318 begin319 FThread.Resume;320 end;321 322 procedure TListedThread.Suspend;323 begin324 FThread.Suspend;325 313 end; 326 314 … … 378 366 379 367 end. 380
Note:
See TracChangeset
for help on using the changeset viewer.