?? 列表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 + -