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.
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.