Solved

Check if App is Running?

Posted on 1998-08-31
13
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
13 Comments
 

Accepted Solution

by:
kashif063098 earned 70 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
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!

 
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

Industry Leaders: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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