Solved

Check if App is Running?

Posted on 1998-08-31
13
172 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 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now