📄 psithread.pas
字号:
unit PsiThread;
//******************************************************************************
// The original software is under
// Copyright (c) 1993 - 2000, Chad Z. Hower (Kudzu)
// and the Indy Pit Crew - http://www.nevrona.com/Indy/
//
// Amended : November 2000, by Michael M. Michalak MACS for use with
// MorphTek.com Inc Peer to Peer Open Source Components - http://www.morphtek.com
//
//******************************************************************************
interface
uses
Classes, SysUtils;
type
TPsiThreadStopMode = (smTerminate, smSuspend);
TPsiExceptionEvent = procedure (Sender: TObject; E: Exception) of object;
TPsiThread = class(TThread)
protected
FData: TObject;
FStopMode: TPsiThreadStopMode;
FStopped: Boolean;
FTerminatingException: string;
FOnException: TPsiExceptionEvent;
function GetStopped: Boolean;
//
procedure Execute; override;
procedure Run; virtual; abstract;
procedure AfterRun; virtual; // Not abstract - otherwise it is required
procedure BeforeRun; virtual; // Not abstract - otherwise it is required
public
constructor Create(ACreateSuspended: Boolean = True); virtual;
destructor Destroy; override;
procedure Start;
procedure Stop;
// Synchronize - Here to expose it
procedure Synchronize(Method: TThreadMethod);
procedure TerminateAndWaitFor; virtual;
property TerminatingException: string read FTerminatingException;
//
property Data: TObject read FData write FData;
property StopMode: TPsiThreadStopMode read FStopMode write FStopMode;
property Stopped: Boolean read GetStopped;
property OnException: TPsiExceptionEvent read FOnException write FOnException;
// Terminated exposed
property Terminated;
end;
TPsiThreadClass = class of TPsiThread;
implementation
uses
PsiGlobal;
procedure TPsiThread.TerminateAndWaitFor;
begin
Terminate;
FStopped := True;
WaitFor;
end;
procedure TPsiThread.AfterRun;
begin
end;
procedure TPsiThread.BeforeRun;
begin
end;
procedure TPsiThread.Execute;
begin
try
while not Terminated do try
if Stopped then begin
Suspended := True; // thread manager will revive us
if Terminated then begin
Break;
end;
end;
BeforeRun;
while not Stopped do begin
Run;
end;
finally
AfterRun;
end;
except
on E: exception do begin
FTerminatingException := E.Message;
if Assigned(FOnException) then
FOnException(self, E);
Terminate;
end;
end;
end;
procedure TPsiThread.Synchronize(Method: TThreadMethod);
begin
inherited Synchronize(Method);
end;
constructor TPsiThread.Create(ACreateSuspended: Boolean);
begin
inherited;
FStopped := ACreateSuspended;
end;
destructor TPsiThread.Destroy;
begin
FreeAndNil(FData);
inherited;
end;
procedure TPsiThread.Start;
begin
if Stopped then begin
// Resume is also called for smTerminate as .Start can be used to initially start a
// thread that is created suspended
FStopped := False;
Suspended := False;
end;
end;
procedure TPsiThread.Stop;
begin
if not Stopped then begin
case FStopMode of
smTerminate: Terminate;
// DO NOT suspend here. Suspend is immediate. See Execute for implementation
smSuspend: ;
end;
FStopped := True;
end;
end;
function TPsiThread.GetStopped: Boolean;
begin
Result := Terminated or FStopped;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -