Board index » delphi » only one instance!

only one instance!

Hej everybody!

When I start a program I want to make sure that there only can be
started one instance of this program. When somebody tries to start
this program a second time while the first instance is still running
the second instance should notice the user.
How can I do this?

If anybody could help me with this?

Regards

Steffen Laukat

 

Re:only one instance!


Example:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    Mutex: THandle;
  end;

[...]

procedure TForm1.FormCreate(Sender: TObject);
begin
  Mutex := CreateMutex(nil, False,
    PChar(UpperCase(ExtractFileName(Application.ExeName))));
  if (Mutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS) then
  begin
    MessageDlg('Application already running.',
      mtError, [mbOk], 0);
    Application.ShowMainForm := False;
    Application.Terminate;
    Exit;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if Mutex <> 0 then
    ReleaseMutex(Mutex);
end;

Quote
"steffen laukat" <stel...@post8.tele.dk> wrote in message

news:pod9pt8fq9mordsatduj28qjqrjl32epkq@4ax.com...
Quote
> Hej everybody!

> When I start a program I want to make sure that there only can be
> started one instance of this program. When somebody tries to start
> this program a second time while the first instance is still running
> the second instance should notice the user.
> How can I do this?

> If anybody could help me with this?

> Regards

> Steffen Laukat

Re:only one instance!


On Tue, 4 Sep 2001 13:48:41 +0200, "M.H. Avegaart"

Quote
<avega...@NOSPAMmccomm.nl> wrote:

Thank you very much for that. I'll try this in a second!

Best regards

Steffen Laukat

Quote
>Example:

>type
>  TForm1 = class(TForm)
>    procedure FormCreate(Sender: TObject);
>    procedure FormDestroy(Sender: TObject);
>  private
>    { Private declarations }
>    Mutex: THandle;
>  end;

>[...]

>procedure TForm1.FormCreate(Sender: TObject);
>begin
>  Mutex := CreateMutex(nil, False,
>    PChar(UpperCase(ExtractFileName(Application.ExeName))));
>  if (Mutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS) then
>  begin
>    MessageDlg('Application already running.',
>      mtError, [mbOk], 0);
>    Application.ShowMainForm := False;
>    Application.Terminate;
>    Exit;
>  end;
>end;

>procedure TForm1.FormDestroy(Sender: TObject);
>begin
>  if Mutex <> 0 then
>    ReleaseMutex(Mutex);
>end;

>"steffen laukat" <stel...@post8.tele.dk> wrote in message
>news:pod9pt8fq9mordsatduj28qjqrjl32epkq@4ax.com...
>> Hej everybody!

>> When I start a program I want to make sure that there only can be
>> started one instance of this program. When somebody tries to start
>> this program a second time while the first instance is still running
>> the second instance should notice the user.
>> How can I do this?

>> If anybody could help me with this?

>> Regards

>> Steffen Laukat

Re:only one instance!


Quote
"M.H. Avegaart" <avega...@NOSPAMmccomm.nl> wrote in message <news:9n2f36$1adm$1@scavenger.euro.net>...
> Example:

> type
>   TForm1 = class(TForm)
>     procedure FormCreate(Sender: TObject);
>     procedure FormDestroy(Sender: TObject);
>   private
>     { Private declarations }
>     Mutex: THandle;
>   end;

> [...]

> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   Mutex := CreateMutex(nil, False,
>     PChar(UpperCase(ExtractFileName(Application.ExeName))));
>   if (Mutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS) then
>   begin
>     MessageDlg('Application already running.',
>       mtError, [mbOk], 0);
>     Application.ShowMainForm := False;
>     Application.Terminate;
>     Exit;
>   end;
> end;

> procedure TForm1.FormDestroy(Sender: TObject);
> begin
>   if Mutex <> 0 then
>     ReleaseMutex(Mutex);
> end

Hi, what about child forms ?

Re:only one instance!


*** post for FREE via your newsreader at post.newsfeeds.com ***

It would be better for you to place the Mutex code in your applications .dpr
file - make it the first thing that happens in your app...

Bryan Ashby

Quote
"M.H. Avegaart" <avega...@NOSPAMmccomm.nl> wrote in message

news:9n2f36$1adm$1@scavenger.euro.net...

Quote
> Example:

> type
>   TForm1 = class(TForm)
>     procedure FormCreate(Sender: TObject);
>     procedure FormDestroy(Sender: TObject);
>   private
>     { Private declarations }
>     Mutex: THandle;
>   end;

> [...]

> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   Mutex := CreateMutex(nil, False,
>     PChar(UpperCase(ExtractFileName(Application.ExeName))));
>   if (Mutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS) then
>   begin
>     MessageDlg('Application already running.',
>       mtError, [mbOk], 0);
>     Application.ShowMainForm := False;
>     Application.Terminate;
>     Exit;
>   end;
> end;

> procedure TForm1.FormDestroy(Sender: TObject);
> begin
>   if Mutex <> 0 then
>     ReleaseMutex(Mutex);
> end;

> "steffen laukat" <stel...@post8.tele.dk> wrote in message
> news:pod9pt8fq9mordsatduj28qjqrjl32epkq@4ax.com...
> > Hej everybody!

> > When I start a program I want to make sure that there only can be
> > started one instance of this program. When somebody tries to start
> > this program a second time while the first instance is still running
> > the second instance should notice the user.
> > How can I do this?

> > If anybody could help me with this?

> > Regards

> > Steffen Laukat

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 90,000 Newsgroups - 16 Different Servers! =-----

Re:only one instance!


Quote
steffen laukat wrote in message ...
>Hej everybody!
>When I start a program I want to make sure that there only can be
>started one instance of this program.
>Steffen Laukat

The mutex solution is proably best but you could also
try this in the MAIN PROJECT FILE:

Program OneInstance;

var Window : THandle;
.//program block
begin
    Window := FindWindow(TMyMainForm, nil);  //
    if Window <> 0 then exit;

   //The standard DELPHI generated code here ...

end;

NOTE : I still use DELPHI 3.0

Re:only one instance!


In article <d5ED7.7145$pK1.140...@juliett.dax.net>, "ELI MOLDEKLEV"

Quote
<eli...@c2i.net> wrote:
>steffen laukat wrote in message ...
>>Hej everybody!
>>When I start a program I want to make sure that there only can be
>>started one instance of this program.
>>Steffen Laukat

>The mutex solution is proably best but you could also
>try this in the MAIN PROJECT FILE:

....

This is also covered in the FAQ, which is posted here every week.
--
Esther Michaels
Please respond to newsgroup; I don't check my mail account

Re:only one instance!


In article <e_r_m-3110011145040...@192.168.0.3>, e_...@my-deja.com (Esther

Quote
Michaels) wrote:
>In article <d5ED7.7145$pK1.140...@juliett.dax.net>, "ELI MOLDEKLEV"
><eli...@c2i.net> wrote:

>>steffen laukat wrote in message ...
>>>Hej everybody!
>>>When I start a program I want to make sure that there only can be
>>>started one instance of this program.
>>>Steffen Laukat

>>The mutex solution is proably best but you could also
>>try this in the MAIN PROJECT FILE:
>....

>This is also covered in the FAQ, which is posted here every week.

Whoops, wrong ng.  It's posted in comp.lang.pascal.delphi.misc!
--
Esther Michaels
Please respond to newsgroup; I don't check my mail account

Re:only one instance!


Quote
"Esther Michaels" <e_...@my-deja.com> wrote in message news:e_r_m-

> Whoops, wrong ng.  It's posted in comp.lang.pascal.delphi.misc!

http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm

Other Threads