tthread.inc 4.8 KB

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