O/H mauro russo έγραψε:
> Dear,
>
> how to check if an .exe is in execution?
>
> In practice I would like to be sure that, if a my application A1 faults
> and is closed, it is automatically restarted.
> I thought to use a secondary very simple deamon application A2 which
> checks if A1 is in execution and, if not, launches it by ShelExecute.
> Moreover A1 does the same with A2.
>
> Really I would like to use a S.O. resource to make A1 and A2 periodically
> "speaking" each other. So, if A1 is running, but hanging, A2 can choose to
> Terminate it and restart it.
>
> I believe there is some simplier way to obtain my goal, but I am not expert
> about windows API.
>
> Some help?
>
> Regards,
> Mauro.
use a named mutex object with the same name in both, your deamon (A2) and your
app (A1).
Your deamon periodically checks for the existence of the mutex.
If the mutex doesn't exist it means the A1 is not running.
That way even if your app moves to another location (folder) your check will be
valid.
program App;
uses
Forms,
Windows,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
const
MutexName = '{04A2E5A9-2CE5-43BC-962B-31DF23C6A3A0}';
var
hMutex : THandle;
begin
hMutex := CreateMutex(nil, False, PChar(MutexName));
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
CloseHandle(hMutex);
end;
end.
------------------------------
Deamon app
function AppIsRunning: Boolean;
const
MutexName = '{04A2E5A9-2CE5-43BC-962B-31DF23C6A3A0}';
var
hMutex : THandle;
begin
hMutex := CreateMutex(nil, False, PChar(MutexName));
try
Result := GetLastError() = ERROR_ALREADY_EXISTS;
finally
CloseHandle(hMutex);
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
if AppIsRunning then
ShowMessage('Running')
else
ShowMessage('NOT Running');
end;
--
Regards
Theo
------------------------
Theo Bebekis
Thessaloniki, Greece
------------------------
The resource you are looking for has been removed,
had its name changed, or is temporarily unavailable.
------------------------
Greek_Delphi_Prog : a Delphi Programming mailing list in Greek at
http://groups.yahoo.com/group/Greek_Delphi_Prog
CSharpDotNetGreek : A C# and .Net mailing list in Greek language at
http://groups.yahoo.com/group/CSharpDotNetGreek
atla_custom : a Unisoft Atlantis Customization mailing list at
http://groups.yahoo.com/group/atla_custom
------------------------
------------------------------------
-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [email protected]! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/delphi-en/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/delphi-en/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:[email protected]
mailto:[email protected]
<*> To unsubscribe from this group, send an email to:
[email protected]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/