tthread.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. constructor TThread.Create(CreateSuspended: Boolean;
  89. const StackSize: SizeUInt = DefaultStackSize);
  90. var
  91. Flags: Integer;
  92. begin
  93. inherited Create;
  94. AddThread;
  95. FSuspended := CreateSuspended;
  96. Flags := 0;
  97. if CreateSuspended then Flags := CREATE_SUSPENDED;
  98. FHandle := BeginThread(nil, StackSize, @ThreadProc, pointer(self), Flags,
  99. FThreadID);
  100. FFatalException := nil;
  101. end;
  102. destructor TThread.Destroy;
  103. begin
  104. if not FFinished and not Suspended then
  105. begin
  106. Terminate;
  107. WaitFor;
  108. end;
  109. if FHandle <> 0 then CloseHandle(FHandle);
  110. FFatalException.Free;
  111. FFatalException := nil;
  112. inherited Destroy;
  113. RemoveThread;
  114. end;
  115. procedure TThread.CallOnTerminate;
  116. begin
  117. FOnTerminate(Self);
  118. end;
  119. procedure TThread.DoTerminate;
  120. begin
  121. if Assigned(FOnTerminate) then
  122. Synchronize(@CallOnTerminate);
  123. end;
  124. const
  125. Priorities: array [TThreadPriority] of Integer =
  126. (THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL,
  127. THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL,
  128. THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL);
  129. function TThread.GetPriority: TThreadPriority;
  130. var
  131. P: Integer;
  132. I: TThreadPriority;
  133. begin
  134. P := GetThreadPriority(FHandle);
  135. Result := tpNormal;
  136. for I := Low(TThreadPriority) to High(TThreadPriority) do
  137. if Priorities[I] = P then Result := I;
  138. end;
  139. procedure TThread.SetPriority(Value: TThreadPriority);
  140. begin
  141. SetThreadPriority(FHandle, Priorities[Value]);
  142. end;
  143. procedure TThread.SetSuspended(Value: Boolean);
  144. begin
  145. if Value <> FSuspended then
  146. if Value then
  147. Suspend else
  148. Resume;
  149. end;
  150. procedure TThread.Suspend;
  151. begin
  152. FSuspended := True;
  153. SuspendThread(FHandle);
  154. end;
  155. procedure TThread.Resume;
  156. begin
  157. if ResumeThread(FHandle) = 1 then FSuspended := False;
  158. end;
  159. procedure TThread.Terminate;
  160. begin
  161. FTerminated := True;
  162. end;
  163. function TThread.WaitFor: Integer;
  164. var
  165. Msg: TMsg;
  166. WaitHandles : array[0..1] of THandle;
  167. begin
  168. if GetCurrentThreadID = MainThreadID then
  169. begin
  170. WaitHandles[0]:=FHandle;
  171. WaitHandles[1]:=THandle(SynchronizeTimeoutEvent);
  172. while true do
  173. begin
  174. case MsgWaitForMultipleObjects(2, WaitHandles, False, INFINITE, QS_SENDMESSAGE) of
  175. WAIT_OBJECT_0:
  176. break;
  177. WAIT_OBJECT_0+1:
  178. CheckSynchronize;
  179. WAIT_OBJECT_0+2:
  180. PeekMessage(Msg, 0, 0, 0, PM_NOREMOVE)
  181. end;
  182. end;
  183. end
  184. else
  185. WaitForSingleObject(ulong(FHandle), INFINITE);
  186. GetExitCodeThread(FHandle, DWord(Result));
  187. end;