📄 列表5.22.txt
字号:
【列表5.22】项目文件OneInst的内容。
program OneInst;
uses
QForms,
SysUtils,
Libc,
QDialogs,
ProcStuff in 'ProcStuff.pas',
frmOneInst in 'frmOneInst.pas' {OneInstMainForm};
const
HOME_DIR : PChar = 'HOME';
BaseLockFileName = '.oneinst. LCK';
var
AlreadyRunning : Boolean;
PID : Integer; { ID for this process }
ProcStatus : String;
UserName : String;
CmdLine : String;
LockPIDStr : String;
LockFile : TextFite;
HomeDirPtr : PChar;
HomeDirName : String;
LockFileName : String;
{SR *.res}
begin
AlreadyRunning := False;
CmdLine := '';
PID := getpid;
HomeDirPtr := getenv(HOME_DIR);
HomeDirName := StrPas(HomeDirPtr);
LockFileName := HomeDirName + '/' + BaseLockFileName;
if FileExists(LockFileName)
then begin
{ Test the file's validity }
AssignFile(LockFile, LockFileName);
Reset(LockFile);
readln(LockFile, LockPIDStr);
CloseFile(LockFile);
{ Is that original process still running -- and
is it this application? }
if GetProcessStatus(StrToInt(LockPIDStr),
ProcStatus, UserName, CmdLine)
= NO_ERR
then begin { It exists. Is it a Zombie? }
if pos('Z'. ProcStatus) = 0
then begin
{ It's running -- and it's not a Zombie. But
is it *this* application? }
if ExtractFileName(CmdLine) =
ExtractFileName(Application. ExeName)
then begin
AlreadyRunning := True;
MessageDlg('Sorry!', 'Sorry. Only '
+ 'one instance of this application '
+ 'is permitted to run at a time.'.
mtError, [mbOK], 0);
end;
end;
end;
end;
if not AlreadyRunning
then begin
{ Create the lock file }
if FileExists(LockFileName) then DeleteFile(LockFileName):
AssignFile(LockFile, LockFileName);
Rewrite(LockFile);
writeln(LockFile, IntToStr(PID));
CloseFile(LockFile);
Application. Initialize;
Application.CreateForm(TOneInstMainForm, OneInstMainForm);
Application. Run;
{ Clean up }
if FileExists(LockFileName) then DeleteFile(LockFileName);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -