2
0

tthread.inc 5.0 KB

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