之前有人问过,但没有完整答案.这与所谓的着名"'致命线程模型!'"有关.
我需要用安全的东西替换这个调用TThread.Suspend,当终止或恢复时返回:
procedure TMyThread.Execute;
begin
while (not Terminated) do begin
if PendingOffline then begin
PendingOffline := false; // flag off.
ReleaseResources;
Self.Suspend; // suspend thread. { evil! ask Barry Kelly why.}
// -- somewhere else, after a long time, a user clicks
// a resume button, and the thread resumes: --
if Terminated then
exit; // leave TThread.Execute.
// Not terminated, so we continue..
GrabResources;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
最初的答案含糊地暗示了"TMutex,TEvent和关键部分".
我想我正在寻找一个TThreadThatDoesntSuck.
以下是带有Win32Event的TThread派生示例,供评论:
unit SignalThreadUnit;
interface
uses
Classes,SysUtils,Windows;
type
TSignalThread …Run Code Online (Sandbox Code Playgroud)