Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check if App is Running?

Posted on 1998-08-31
13
Medium Priority
?
212 Views
Last Modified: 2010-04-04
How can i check if the application is already running before i run it again.
bozo
0
Comment
Question by:bozo7
  • 3
  • 3
  • 2
  • +3
13 Comments
 

Accepted Solution

by:
kashif063098 earned 210 total points
ID: 1338231
answer is coming
0
 

Expert Comment

by:kashif063098
ID: 1338232
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
 

Expert Comment

by:chrismo
ID: 1338233
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:viktornet
ID: 1338234
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1338235
bozo7,

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

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1338236
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
 

Expert Comment

by:chrismo
ID: 1338237
FWIW, I read the hPrevInst will not work in Win32...
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1338238
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
 
LVL 1

Author Comment

by:bozo7
ID: 1338239
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
 

Expert Comment

by:chrismo
ID: 1338240
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
 

Expert Comment

by:kashif063098
ID: 1338241
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1338242
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
 
LVL 1

Author Comment

by:bozo7
ID: 1338243
Thanks for Help
bozo
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question