Board index » delphi » One instance of APP only

One instance of APP only

In the TI documents provided by Borland/Inprise, an example is given on how
to get an application to stop a second instance of itself being executed.

This example doesn't seem to work in Delphi 4. Is there any other way of
doing this. (It must work on NT also).

Thanks!

 

Re:One instance of APP only


:Don Schoeman <dpds...@mweb.co.za> schrieb in im Newsbeitrag: 3898986...@news1.mweb.co.za...
: In the TI documents provided by Borland/Inprise, an example is given on how
: to get an application to stop a second instance of itself being executed.
:
: This example doesn't seem to work in Delphi 4. Is there any other way of
: doing this. (It must work on NT also).
:
: Thanks!
:
:

I don't know what Inprise provided there but here's an example of this NG using an OS object called "Mutex":

uses
    {...}
    Windows;
var
 hMutex:THandle;
begin
     hMutex:=CreateMutex(nil, false, 'MyMutex');
     if WaitForSingleObject(hMutex,0)<>wait_TimeOut then begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      {...}
      Application.Run;
 end;
  ReleaseMutex(hMutex);
end.

Hth,
Matthias.

Re:One instance of APP only


Here is what I do to prevent a second instance of an application to be
created.  In the code of the project, I write:

var
    hMutex: THandle;
begin
    hMutex:= CreateMutex(nil, flase, 'MyMutex');
    if WaitForSingleInstance(hMutex, 0) <> wait_TimeOut then
    begin
        Application Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
    end;
end.

With this scheme, a Mutex (mutual exclusion object) is created when the
application is instanciated the first time.  If the application is run for
the second time, a temporary copy of the application is created but
destroyed immediately when the time-out elapses.

Hope it helps.

Georges

Quote
Don Schoeman <dpds...@mweb.co.za> wrote in message

news:38989864.0@news1.mweb.co.za...
Quote
> In the TI documents provided by Borland/Inprise, an example is given on
how
> to get an application to stop a second instance of itself being executed.

> This example doesn't seem to work in Delphi 4. Is there any other way of
> doing this. (It must work on NT also).

> Thanks!

Other Threads