- Timestamp:
- May 24, 2011, 12:34:59 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/UThreading.pas
r245 r246 12 12 TMethodCall = procedure of object; 13 13 14 15 { TVirtualThread } 16 17 TVirtualThread = class 18 private 19 function GetFreeOnTerminate: Boolean; virtual; abstract; 20 function GetPriority: TThreadPriority; virtual; abstract; 21 function GetSuspended: Boolean; virtual; abstract; 22 function GetTerminated: Boolean; virtual; abstract; 23 function GetThreadId: Integer; virtual; abstract; 24 procedure SetFreeOnTerminate(const AValue: Boolean); virtual; abstract; 25 procedure SetPriority(const AValue: TThreadPriority); virtual; abstract; 26 procedure SetSuspended(const AValue: Boolean); virtual; abstract; 27 procedure SetTerminated(const AValue: Boolean); virtual; abstract; 28 public 29 Name: string; 30 procedure Execute; virtual; abstract; 31 procedure Resume; virtual; abstract; 32 procedure Suspend; virtual; abstract; 33 procedure Start; virtual; abstract; 34 procedure Terminate; virtual; abstract; 35 procedure Synchronize(AMethod: TThreadMethod); virtual; abstract; 36 procedure WaitFor; virtual; abstract; 37 procedure Sleep(Delay: Integer); virtual; abstract; 38 property FreeOnTerminate: Boolean read GetFreeOnTerminate 39 write SetFreeOnTerminate; 40 property Suspended: Boolean read GetSuspended 41 write SetSuspended; 42 property Priority: TThreadPriority read GetPriority write SetPriority; 43 property Terminated: Boolean read GetTerminated write SetTerminated; 44 property ThreadId: Integer read GetThreadId; 45 end; 46 47 TListedThread = class; 48 49 { TListedThreadExecute } 50 51 TListedThreadExecute = class(TThread) 52 Parent: TListedThread; 53 procedure Execute; override; 54 end; 55 14 56 { TListedThread } 15 57 16 TListedThread = class(TThread) 17 Name: string; 58 TListedThread = class(TVirtualThread) 59 private 60 FTerminated: Boolean; 61 FThread: TListedThreadExecute; 62 function GetFreeOnTerminate: Boolean; override; 63 function GetPriority: TThreadPriority; override; 64 function GetSuspended: Boolean; override; 65 function GetTerminated: Boolean; override; 66 function GetThreadId: Integer; override; 67 procedure SetFreeOnTerminate(const AValue: Boolean); override; 68 procedure SetPriority(const AValue: TThreadPriority); override; 69 procedure SetSuspended(const AValue: Boolean); override; 70 procedure SetTerminated(const AValue: Boolean); override; 71 public 18 72 constructor Create(CreateSuspended: Boolean; 19 73 const StackSize: SizeUInt = DefaultStackSize); 20 74 destructor Destroy; override; 21 procedure Sleep(Delay: Integer); 22 property Terminated; 75 procedure Sleep(Delay: Integer); override; 76 procedure Execute; override; 77 procedure Resume; override; 78 procedure Suspend; override; 79 procedure Start; override; 80 procedure Terminate; override; 81 procedure Synchronize(AMethod: TThreadMethod); override; 82 procedure WaitFor; override; 23 83 end; 24 84 … … 33 93 end; 34 94 35 var 36 ThreadList: TObjectList; // TList<TListedThread> 95 { TThreadList } 96 97 TThreadList = class(TObjectList) 98 function FindById(Id: Integer): TVirtualThread; 99 constructor Create; 100 end; 101 102 var 103 ThreadList: TThreadList; 37 104 ThreadListLock: TCriticalSection; 38 105 OnException: TExceptionEvent; … … 42 109 43 110 resourcestring 44 SCurrentThreadNotFound = 'Current thread ID %d not found in list.';111 SCurrentThreadNotFound = 'Current thread ID %d not found in virtual thread list.'; 45 112 46 113 … … 68 135 var 69 136 I: Integer; 70 Thread: T ListedThread;137 Thread: TVirtualThread; 71 138 begin 72 139 if MainThreadID = ThreadID then Method 73 140 else begin 74 I := 0; 75 while (I < ThreadList.Count) and (TListedThread(ThreadList[I]).ThreadID <> ThreadID) do 76 Inc(I); 77 if I < ThreadList.Count then begin 78 Thread := TListedThread(ThreadList[I]); 79 TThread.Synchronize(Thread, Method); 141 Thread := ThreadList.FindById(ThreadID); 142 if Assigned(Thread) then begin 143 Thread.Synchronize(Method); 80 144 end else raise Exception.Create(Format(SCurrentThreadNotFound, [ThreadID])); 81 145 end; 82 146 end; 83 147 148 { TVirtualThread } 149 150 151 { TThreadList } 152 153 function TThreadList.FindById(Id: Integer): TVirtualThread; 154 var 155 I: Integer; 156 begin 157 I := 0; 158 while (I < ThreadList.Count) and (TVirtualThread(ThreadList[I]).ThreadID <> Id) do 159 Inc(I); 160 if I < ThreadList.Count then Result := TVirtualThread(ThreadList[I]) 161 else Result := nil; 162 end; 163 164 constructor TThreadList.Create; 165 begin 166 inherited Create; 167 OwnsObjects := False; 168 end; 169 170 { TListedThreadExecute } 171 172 procedure TListedThreadExecute.Execute; 173 begin 174 try 175 Parent.Execute; 176 except 177 on E: Exception do 178 if Assigned(OnException) then 179 OnException(Parent.FThread, E); 180 end; 181 end; 182 183 { TVirtualThread } 184 185 84 186 { TListedThread } 187 188 function TListedThread.GetFreeOnTerminate: Boolean; 189 begin 190 Result := FThread.FreeOnTerminate; 191 end; 192 193 function TListedThread.GetPriority: TThreadPriority; 194 begin 195 Result := FThread.Priority; 196 end; 197 198 function TListedThread.GetSuspended: Boolean; 199 begin 200 Result := FThread.Suspended; 201 end; 202 203 function TListedThread.GetTerminated: Boolean; 204 begin 205 Result := FTerminated; 206 end; 207 208 function TListedThread.GetThreadId: Integer; 209 begin 210 Result := FThread.ThreadID; 211 end; 212 213 procedure TListedThread.SetFreeOnTerminate(const AValue: Boolean); 214 begin 215 FThread.FreeOnTerminate := AValue; 216 end; 217 218 procedure TListedThread.SetPriority(const AValue: TThreadPriority); 219 begin 220 FThread.Priority := AValue; 221 end; 222 223 procedure TListedThread.SetSuspended(const AValue: Boolean); 224 begin 225 FThread.Suspended := AValue; 226 end; 227 228 procedure TListedThread.SetTerminated(const AValue: Boolean); 229 begin 230 FTerminated := AValue; 231 if AValue then FThread.Terminate; 232 end; 85 233 86 234 constructor TListedThread.Create(CreateSuspended: Boolean; 87 235 const StackSize: SizeUInt); 88 236 begin 89 inherited; 237 FThread := TListedThreadExecute.Create(True, StackSize); 238 FThread.Parent := Self; 90 239 try 91 240 ThreadListLock.Acquire; … … 94 243 ThreadListLock.Release; 95 244 end; 245 if CreateSuspended = False then FThread.Start; 96 246 end; 97 247 … … 109 259 ThreadListLock.Release; 110 260 end; 261 FThread.Free; 111 262 inherited Destroy; 112 263 end; … … 123 274 SysUtils.Sleep(Quantum); 124 275 end; 276 end; 277 278 procedure TListedThread.Execute; 279 begin 280 end; 281 282 procedure TListedThread.Resume; 283 begin 284 FThread.Resume; 285 end; 286 287 procedure TListedThread.Suspend; 288 begin 289 FThread.Suspend; 290 end; 291 292 procedure TListedThread.Start; 293 begin 294 FThread.Start; 295 end; 296 297 procedure TListedThread.Terminate; 298 begin 299 FTerminated := True; 300 FThread.Terminate; 301 end; 302 303 procedure TListedThread.Synchronize(AMethod: TThreadMethod); 304 begin 305 FThread.Synchronize(FThread, AMethod); 306 end; 307 308 procedure TListedThread.WaitFor; 309 begin 310 FThread.WaitFor; 125 311 end; 126 312 … … 142 328 143 329 ThreadListLock := TCriticalSection.Create; 144 ThreadList := TObjectList.Create; 145 ThreadList.OwnsObjects := False; 330 ThreadList := TThreadList.Create; 146 331 147 332 finalization
Note:
See TracChangeset
for help on using the changeset viewer.