Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check if app already running?

Status
Not open for further replies.

LucieLastic

Programmer
May 9, 2001
1,694
GB
hi All

I did this many moons ago for a screen saver type program but can't remember how, the old grey cells are letting me down again.

How do you check if an app is already running ? In this case we want to check Excel isn't already running. I'm thinking of CreateProcess for some reason, then FindWindow....bah, humbug! can't remember....

Many thanks for help
lou
 
write following code in project file

program Project1;

uses
Forms,
Dialogs,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';

{$R *.res}

begin

if OnlyOneRunning then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end.

write following code in Unit1 having main form

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
//ShowMessage(String(UniqueAppStr));
ShowMessage(String(unit2.UniqueAppStr));
end;

end.

write following code in Unit2
unit Unit2;

interface

function OnlyOneRunning:Boolean;

implementation
uses windows;

function OnlyOneRunning:Boolean;
var
myMutex:THandle;
begin
myMutex:=createmutex(nil, False,'MyUniqueProgramNameGoesHere');
Result:=WaitForSingleObject(myMutex,0) <> wait_timeout;
end;

end.
 
write following code in project file

program Project1;

uses
Forms,
Dialogs,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';

{$R *.res}

begin

if OnlyOneRunning then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end.

write following code in Unit1 having main form

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
ShowMessage(String(unit2.UniqueAppStr));
end;

end.

write following code in Unit2
unit Unit2;

interface

function OnlyOneRunning:Boolean;

implementation
uses windows;

function OnlyOneRunning:Boolean;
var
myMutex:THandle;
begin
myMutex:=createmutex(nil, False,'MyUniqueProgramNameGoesHere');
Result:=WaitForSingleObject(myMutex,0) <> wait_timeout;
end;

end.
 
CreateMutex, ofcourse! Many thanks, Saif, all comes flooding back now :eek:)

lou
 
Just out of interest, how do you use CreateMutex to determine whether or not Excel is running?
 
hi Mike

I think you're right, you can't find out if Excel is running from here. As I'm not working on this bit yet I hadn't given it much thought. The Mutex eg. is for checking instances of one's app.

I think FindWindow is looking the best option at the mo., do you have any suggestions ?

thanks
lou
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top