PDA

View Full Version : چک کردن در حال اجرا بودن یا نبودن یک برنامه



alirzn
پنج شنبه 01 آذر 1386, 08:11 صبح
من یک تو برنامم می خوام چک کنم که یک برنامه exe دیگه در حال حاضر در حال اجراست یا خیر؟ چطور میتونم اینو چک کنم؟

as13851365
پنج شنبه 01 آذر 1386, 10:21 صبح
اگر منظور شما همان برنامه باشد یعنی چک کنه که یک نسخه دیگه از خودش در حال اجرا است یا نه می تونید از کد زیر استفاده کنید:


program background;

uses
Forms,
windows,
Unit1 in 'Unit1.pas' {Form1};

var run:thandle;
{$R *.res}

begin
run:=createmutex(nil,false,'name_barname');
if waitforsingleobject(run,0)<>wait_timeout then
begin
Application.Initialize;
Application.ShowMainForm:=false;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end.

AmirEngineer
پنج شنبه 01 آذر 1386, 10:57 صبح
این برنامه یک فایل exe رو اجرا میکنه و بعد از بسته شدن اون فایل exe مدت زمان اجرای اون رو نشون میده , اگه interval تایمر 1000 باشه اینن زمان بر حسب ثانیه است.


var
Form1: TForm1;
I:Integer;
HMutex:THandle;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
I:=0;
WinExec('Project.exe',SW_HIDE);
HMutex:=CreateMutex(nil,False,'Project.exe');
End;
procedure TForm1.Timer1Timer(Sender: TObject);
Begin
IF WaitForSingleObject(HMutex,0)=WAIT_TIMEOUT Then
Begin
I:=I+1;
End
Else
Begin
Timer1.Enabled:=False;
Label1.Caption:=IntToStr(I);
End;
end;

end.