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!


a) ask in a Delphi newsgroup
b) let the program create a lockfile at the beginning and let it be erased
at the end of execution. If the lockfile exists, stop starting up the
program. Netscape uses a similar technique on linux. The only problem is, if
your computer crashes while there is still the file on your disk, the
program can't be started anymore.

Leo Nesemann

Re:only one instance!


Quote
Leo Nesemann wrote:
> a) ask in a Delphi newsgroup
> b) let the program create a lockfile at the beginning and let it be erased
> at the end of execution. If the lockfile exists, stop starting up the
> program. Netscape uses a similar technique on linux. The only problem is, if
> your computer crashes while there is still the file on your disk, the
> program can't be started anymore.

> Leo Nesemann

Set some environement variable which gets checked at program start. If
it is already present, you wrnd the user. After a reset of the PC all is
okay again. The envar can be read with GETENV, but for setting one you
must search some code on the internet (it exists) or ask here for it.

Markus

Re:only one instance!


steffen laukat schreef:

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

If you are using TPW 1.5 (or similar OOP), try this:

program OnlyOne;

uses Wintypes, Winprocs, WObjects;

type

NoMultiApplication= object(TApplication)
  FirstInstance:boolean;
  constructor Init(AName: PChar);
  procedure InitMainWindow; virtual;
  procedure InitApplication; virtual;
end;

constructor NoMultiApplication.Init(AName: PChar);
begin
  FirstInstance:=false;
  TApplication.Init(AName)
end;

procedure NoMultiApplication.InitApplication;
begin
  FirstInstance:=true
end;

procedure NoMultiApplication.InitMainWindow;
begin
  if FirstInstance
     then begin
            MainWindow:=New(PWindow, Init(nil,
                         'First (and only) instance'))  
          end
     else begin
            Messagebox(0, 'Multiple instances not allowed',
                   'Application Error',
                    mb_TaskModal or mb_IconExclamation);
            Halt(0) {only this unwanted instance}
          end          
end; {NoMultiApplication.InitMainWindow}

var  NoMultiApp:NoMultiApplication

begin
  NoMultiApp.Init('NoMulti');
  NoMultiApp.Run;
  NoMultiApp.Done
end.

Quote
>From / thanks to:

TP for Win 3.0 programming, Tom Swan,
ISBN 0-553-35293-8, page 155-161

Meaning:
Since InitApplication is called once prior to creating
the first instance and InitMainWindow is only called to
set up a new adress space for every new instance,
you can modify a field (boolean variable) called
FirstInstance trough the above two virtual methods.

The system variable HPrevInst seems to have the same effect.
(HPrevInst<>0 => this is not the first instance)

I hope this is not a homework-assignment.

Greetings. Huub.

Re:only one instance!


Hej again!

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

Best regards

Steffen Laukat

Re:only one instance!


This simple method can be used to prevent multiple use of a program, or
multiple use of a given set of data files on a network. I use simple
text files to store flags. For example, we have several different
inventories in use on our server. Each inventory has a dedicated flag
file. If an inventory is open with a DOS program, the flag file contains
the ID of the user. When a second person tries to access the same file,
they get a message that says, "File Already Open By User On John's PC.
Press Any Key To Exit."

When the first user closes the files, the flag "CLOS" is stored in the
flag file, to indicate that the files are closed and may be opened for
access. (I often have to go to my boss's PC and close an inventory file
so I can access it from my own.)

For the inventories accessed through Windows programs, the flag file is
used to limit read/write access to the first user to open the file and
allow read-only access to anyone else. (DOS based inventories are being
phased out slowly because they work so well that it isn't worth the
effort to transfer the data.)

Using Databases (like Paradox, Excell, etc.) provides built in access
controls but I have found that the extra work to write and maintain code
needed for databases is far more trouble to me than it's worth.

Quote
steffen laukat 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

--
Val Mehling
va...@earthlink.net

Web Pages
http://home.earthlink.net/~valjm/

XRL (Extreme Racing League) Pics
http://odin.prohosting.com/~valjm

http://odin.prohosting.com/valmeh/

Other Threads