tthread.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. { Thread management routines }
  2. const
  3. CM_EXECPROC = $8FFF;
  4. CM_DESTROYWINDOW = $8FFE;
  5. type
  6. PRaiseFrame = ^TRaiseFrame;
  7. TRaiseFrame = record
  8. NextRaise: PRaiseFrame;
  9. ExceptAddr: Pointer;
  10. ExceptObject: TObject;
  11. ExceptionRecord: pointer; {PExceptionRecord}
  12. end;
  13. var
  14. ThreadWindow: HWND;
  15. ThreadCount: Integer;
  16. { event that happens when gui thread is done executing the method
  17. }
  18. function ThreadWndProc(Window: HWnd; AMessage:UInt; WParam : WParam; LParam: LParam): Longint; stdcall;
  19. begin
  20. case AMessage of
  21. CM_EXECPROC:
  22. with TThread(lParam) do
  23. begin
  24. Result := 0;
  25. try
  26. FSynchronizeException := nil;
  27. FMethod;
  28. except
  29. { if RaiseList <> nil then
  30. begin
  31. FSynchronizeException := PRaiseFrame(RaiseList)^.ExceptObject;
  32. PRaiseFrame(RaiseList)^.ExceptObject := nil;
  33. end; }
  34. end;
  35. end;
  36. CM_DESTROYWINDOW:
  37. begin
  38. DestroyWindow(Window);
  39. Result := 0;
  40. end;
  41. else
  42. Result := DefWindowProc(Window, AMessage, wParam, lParam);
  43. end;
  44. end;
  45. const
  46. ThreadWindowClass: TWndClass = (
  47. style: 0;
  48. lpfnWndProc: nil;
  49. cbClsExtra: 0;
  50. cbWndExtra: 0;
  51. hInstance: 0;
  52. hIcon: 0;
  53. hCursor: 0;
  54. hbrBackground: 0;
  55. lpszMenuName: nil;
  56. lpszClassName: 'TThreadWindow');
  57. procedure AddThread;
  58. function AllocateWindow: HWND;
  59. var
  60. TempClass: TWndClass;
  61. ClassRegistered: Boolean;
  62. begin
  63. ThreadWindowClass.hInstance := HInstance;
  64. ThreadWindowClass.lpfnWndProc:=WndProc(@ThreadWndProc);
  65. ClassRegistered := GetClassInfo(HInstance, ThreadWindowClass.lpszClassName,
  66. @TempClass);
  67. if not ClassRegistered or (TempClass.lpfnWndProc <> WndProc(@ThreadWndProc)) then
  68. begin
  69. if ClassRegistered then
  70. Windows.UnregisterClass(ThreadWindowClass.lpszClassName, HInstance);
  71. Windows.RegisterClass(ThreadWindowClass);
  72. end;
  73. Result := CreateWindow(ThreadWindowClass.lpszClassName, '', 0,
  74. 0, 0, 0, 0, 0, 0, HInstance, nil);
  75. end;
  76. begin
  77. if ThreadCount = 0 then
  78. ThreadWindow := AllocateWindow;
  79. Inc(ThreadCount);
  80. end;
  81. procedure RemoveThread;
  82. begin
  83. Dec(ThreadCount);
  84. if ThreadCount = 0 then
  85. PostMessage(ThreadWindow, CM_DESTROYWINDOW, 0, 0);
  86. end;
  87. { TThread }
  88. function ThreadProc(ThreadObjPtr: Pointer): PtrInt;
  89. var
  90. FreeThread: Boolean;
  91. Thread: TThread absolute ThreadObjPtr;
  92. begin
  93. try
  94. Thread.Execute;
  95. except
  96. Thread.FFatalException := TObject(AcquireExceptionObject);
  97. end;
  98. FreeThread := Thread.FFreeOnTerminate;
  99. Result := Thread.FReturnValue;
  100. Thread.FFinished := True;
  101. Thread.DoTerminate;
  102. if FreeThread then Thread.Free;
  103. end;
  104. constructor TThread.Create(CreateSuspended: Boolean;
  105. const StackSize: SizeUInt = DefaultStackSize);
  106. var
  107. Flags: Integer;
  108. begin
  109. inherited Create;
  110. AddThread;
  111. FSuspended := CreateSuspended;
  112. Flags := 0;
  113. if CreateSuspended then Flags := CREATE_SUSPENDED;
  114. FHandle := BeginThread(nil, StackSize, @ThreadProc, pointer(self), Flags,
  115. FThreadID);
  116. FFatalException := nil;
  117. end;
  118. destructor TThread.Destroy;
  119. begin
  120. if not FFinished and not Suspended then
  121. begin
  122. Terminate;
  123. WaitFor;
  124. end;
  125. if FHandle <> 0 then CloseHandle(FHandle);
  126. FFatalException.Free;
  127. FFatalException := nil;
  128. inherited Destroy;
  129. RemoveThread;
  130. end;
  131. procedure TThread.CallOnTerminate;
  132. begin
  133. FOnTerminate(Self);
  134. end;
  135. procedure TThread.DoTerminate;
  136. begin
  137. if Assigned(FOnTerminate) then
  138. Synchronize(@CallOnTerminate);
  139. end;
  140. const
  141. Priorities: array [TThreadPriority] of Integer =
  142. (THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
  143. THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL,
  144. THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL);
  145. function TThread.GetPriority: TThreadPriority;
  146. var
  147. P: Integer;
  148. I: TThreadPriority;
  149. begin
  150. P := GetThreadPriority(FHandle);
  151. Result := tpNormal;
  152. for I := Low(TThreadPriority) to High(TThreadPriority) do
  153. if Priorities[I] = P then Result := I;
  154. end;
  155. procedure TThread.SetPriority(Value: TThreadPriority);
  156. begin
  157. SetThreadPriority(FHandle, Priorities[Value]);
  158. end;
  159. procedure TThread.SetSuspended(Value: Boolean);
  160. begin
  161. if Value <> FSuspended then
  162. if Value then
  163. Suspend else
  164. Resume;
  165. end;
  166. procedure TThread.Suspend;
  167. begin
  168. FSuspended := True;
  169. SuspendThread(FHandle);
  170. end;
  171. procedure TThread.Resume;
  172. begin
  173. if ResumeThread(FHandle) = 1 then FSuspended := False;
  174. end;
  175. procedure TThread.Terminate;
  176. begin
  177. FTerminated := True;
  178. end;
  179. function TThread.WaitFor: Integer;
  180. var
  181. Msg: TMsg;
  182. WaitHandles : array[0..1] of THandle;
  183. begin
  184. if GetCurrentThreadID = MainThreadID then
  185. begin
  186. WaitHandles[0]:=FHandle;
  187. WaitHandles[1]:=THandle(SynchronizeTimeoutEvent);
  188. while true do
  189. begin
  190. case MsgWaitForMultipleObjects(2, WaitHandles, False, INFINITE, QS_SENDMESSAGE) of
  191. WAIT_OBJECT_0:
  192. break;
  193. WAIT_OBJECT_0+1:
  194. CheckSynchronize;
  195. WAIT_OBJECT_0+2:
  196. PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE)
  197. end;
  198. end;
  199. end
  200. else
  201. WaitForSingleObject(ulong(FHandle), INFINITE);
  202. GetExitCodeThread(FHandle, DWord(Result));
  203. end;