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!


Hello, "steffen laukat" <stel...@post8.tele.dk>! You wrote:

Quote
> 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?

   See for the mutexes in WinAPI.

--
    Evgeny V. Levashov, Software Developer,
    ICQ UIN 34864288
    Mebel Massiv Co, Tula, Russia
    http://home.tula.net/mebelmassiv    

Re:only one instance!


Hi,

See this piece of code :

Function IsPrevInstance:HWND;

implementation

Function IsPrevInstance:HWND;
// cette fonction renvoie 0 s'il n'y a pas d'instance du mme programme dj
lance sinon
//le handle de l'instance dj lance c'est dire une valeur <>0
//returns 0 if no other instance of the program has already been loaded
// cette fonction est appel dans le source du projet. voir ce source pour
comprendre
// this function is used in the source of the project. You have to check if
another instance of the program already exists and if it is the case, to
activate this instance and close the program.
Var ClassName:Array[0..255] of char;
    TitreApplication:string;
Begin
result:=0;
TitreApplication:=Application.Title;
Application.Title:='';
 try
   GetClassName(Application.handle, ClassName, 254); class
   result:=FindWindow(ClassName,PChar(TitreApplication));
 finally
  Application.Title:=TitreApplication;
 end;
end;

For more details, take a look at http://perso.wanadoo.fr/bardou/michel/

Re:only one instance!


Try putting this as the first lines in your .dpr file

Cheers
don

var hMyMutex : tHandle;

begin
  hMyMutex := CreateMutex (nil, True,pChar (Uppercase (ExtractFileName
(Application.ExeName))));
  if (hMyMutex = 0) or (GetLastError = error_Already_Exists) then
  begin
    ShowMessage('Program Already Running');
    Application.Terminate;
  end

  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;

  ReleaseMutex (hMyMutex);
end.

On Tue, 04 Sep 2001 13:08:40 +0200, steffen laukat

Quote
<stel...@post8.tele.dk> wrote:
>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, 04 Sep 2001 13:08:40 +0200, steffen laukat

Quote
<stel...@post8.tele.dk> wrote:

Hej again!

Thank you very much for your help. I found what I needed.

Best regards

Steffen Laukat

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!


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

news:bhd9pto1e4f73lar7grcvusteosvjlimep@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?

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

Other Threads