Check if App is Running?

How can i check if the application is already running before i run it again.
bozo
LVL 1
bozo7Asked:
Who is Participating?
 
kashif063098Commented:
answer is coming
0
 
kashif063098Commented:
The simple way is to use win api FindWindow() ,to find out if an application is already running.If you need further help let me know
0
 
chrismoCommented:
A more reliable method is as follows:

uses     Windows;
var   Mutex: THandle;

begin
  Mutex:= CreateMutex(nil, True, 'BillyTheBad');
  if GetLastError = ERROR_ALREADY_EXISTS then    
  begin
    if Mutex <> 0 then    
      CloseHandle(Mutex);
  end
  else begin
    ... rest of .dpr code
  end;

end.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
viktornetCommented:
Here is code for for running only once....
--------------
uses Windows;

ResourceString
        RSCantStart = 'DME is already running. Can not start another
copy.';
   RSTitle = 'Document Manager';

implementation

const
        TEN_SECONDS = 10000;

var
        MutexHandle : integer;

initialization
   MutexHandle := CreateMutex(Nil, False, 'DME Local Server Mutex.');
   if WaitForSingleObject(MutexHandle, TEN_SECONDS) <> WAIT_OBJECT_0
then
   begin
      MutexHandle := -1;
      Messagebox(0, PChar(RSCantStart), PChar(RSTitle), MB_OK);
      Halt;
   end;

finalization
        if MutexHandle <> -1 then
        ReleaseMutex(MutexHandle);
end.
---------------
Regards,
Viktor Ivanov
0
 
MadshiCommented:
bozo7,

do you mean your own (delphi) application, or do you mean another "alien" application?

Regards, Madshi.
0
 
BlackDeathCommented:
hi, bozo!

if you mean a previous instance of your app, try the following:

procedure TForm1.FormCreate(Sender: TObject);
begin
   if hPrevinst <> 0 then
      begin
      ShowMessage('A previous instance was running I am getting out');
      PostQuitmessage(0);
      end;
end;

(this came from Delphi Knowledge Base System help file)

regs,
Black Death.
0
 
chrismoCommented:
FWIW, I read the hPrevInst will not work in Win32...
0
 
BlackDeathCommented:
whoops!

you're right. sorry. i used it in an older d1-app, didn't realize it wouldn't work with win32...

so long,
Black Death.
0
 
bozo7Author Commented:
Kashif
what would i put for the parameters of the findwindow()
the name of the program is systra.exe

Chrismo
i get an error illegal character in input file:''($A0)
on line "if getlasterror = Error_Already_Exist"

This is my own application that i am checking if it is already running. It puts an icon into the system tray and it is possible that this exe could be started by more than one other program. So we don't want multiple icons in sys tray.
Thanks for Help
Bozo
0
 
chrismoCommented:
Illegal character tells me you've just typoed something - like you have a closing brace } that maybe you forgot to remove ... but not being able to see the code, I couldn't say for sure.
0
 
kashif063098Commented:
bozo7

When you run systra.exe whats the caption of the main window ??

if its systra then
var
  h:Hwnd;

  h:=FindWindow(Nil,'systra');
  if (h = 0) then ShowMessage('Application not Found');

be sure to include windows in uses clause

any further assistance let me know.


0
 
viktornetCommented:
Example:

To get the handle of the your aplication you can use two methods....

FindWindow(nil, 'caption');
FindWindow('TForm1', nil);
//TForm1 is the class of your form if you don;t change the form's name and stuff like that....
the whole code would be......

var
  H : HWND;
begin
  H := FindWindow(nil, 'My Form''s caption');//This would be like Untitled - Notepad
  if H<>0 then
  begin
    ShowMessage('There is already another instance of this application running!')
    SendMessage(Handle, WM_CLOSE, 0,0)
  end;
end;

Hope this helps...

Regards,
Viktor Ivanov
0
 
bozo7Author Commented:
Thanks for Help
bozo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.