We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Check if App is Running?

bozo7
bozo7 asked
on
Medium Priority
226 Views
Last Modified: 2010-04-04
How can i check if the application is already running before i run it again.
bozo
Comment
Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
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

Commented:
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.
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

Commented:
bozo7,

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

Regards, Madshi.
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.

Commented:
FWIW, I read the hPrevInst will not work in Win32...
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.

Author

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

Commented:
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.
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.


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

Author

Commented:
Thanks for Help
bozo
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.