Solved

Allow only one instance of the program to be started on network.

Posted on 2007-11-29
12
180 Views
Last Modified: 2010-04-21
Hi,

My application is installed on a network-drive. End-users can startup this application from their local computer using a shortcut. ** I don't want to allow multiple instances of my application to be started.

After user-A has started the application on local computer-1, it is not sufficient to check for a running instance of this application on local computer-2, because that instance will not be recognized on local computer-2 (as it is running locally on computer-1).
I've thought about creating a controle-file the moment the application is started by any user/local computer and delete this file after the application is closed again. But this might give some timing-problems when two users start the application at the same time.

How can I solve this problem in an easy but certain/secure way?

Regards, Stef

Open in new window

0
Comment
Question by:Delphiwizard
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20374160
your idea of a 'lock' file is probably the simplest idea.
If you create the file with a TFileStream, with mode fmShareExclusive, you are sure only one user can open the file.

from the help:

fmShareExclusive Other applications can not open the file for any reason.
 
0
 

Author Comment

by:Delphiwizard
ID: 20374399
It would be nice to have a bit more of a solution (code example).
In other words: how to handle checking for an existing instance of the application?
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 20374954
0
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
ID: 20374971
This will do what you want but you will need to give some thought to where you create the InUse.Fil. This code give no consideration to the posibilty that the application is in more than one place.

procedure TForm1.FormCreate(Sender: TObject);

begin

	try

	_gFileStream := TFileStream.Create( ExtractFilePath(Application.ExeName) + 'InUse.fil',fmCreate or fmShareExclusive);

	except

		ShowMessage('Denied Access');

		timerClose.Enabled := true;

	end;

end;
 

procedure TForm1.FormDestroy(Sender: TObject);

begin

	_gFileStream.Free;

end;
 

procedure TForm1.timerCloseTimer(Sender: TObject);

begin

	Close;

end;

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 20375168
SteveBay: What is the use of the timer, I don't see where it is actually used in your code?
0
 

Author Comment

by:Delphiwizard
ID: 20375415
Another question.
What if the application crashes because of an error, will 'InUse.fil' be freed?
If not: how to free it?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 500 total points
ID: 20375441
The timer will close your application.  . When you are unable to create the file an exception is raised and the close timer is enabled.

It is a graceful way to close an applicaiton from the MainForm OnShow or OnCreate event. Set the Timer to a short interval (50 or so) and diabled in the designer and enable when you wish to force the applicaiton to close.
0
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 500 total points
ID: 20375481
If the applicaiton crashes it should no longer holds the file open exclusively. You should be able to restart the applicaiotn with no problem.
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20375494
Just a question, but if your application crashes there is no graceful shutdown, no other events in the app are processed and I believe that the InUse.fil will still be there and Windows will still show the file as locked.

No other app will run since it is still locked or until Windows does a reboot.

I am not certain if Windows releases this kind of locking after a crash.

Just a comment for my further understanding if anyone knows!

John

0
 

Author Closing Comment

by:Delphiwizard
ID: 31411677
Thank you very much for your help.
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 20375537
That is difficult to test, however, I crashed the program with an access violation and Task manager and the file was no longer in use. I believe that there are circumstances where the OS will continue to hold the file after the application crashes. Then the machine that was running the application may have to be logged off and back on.
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20375579
Thanks Steve for the reply. I believe that to be true also and was wondering about that. I have had the same results; sometimes still locked and other times no problem.

Gotta love Windows! :)

John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 189
Simple Delphi Question 9 83
Why is there a Difference between the SQL String and using the Parameter ? 7 30
CheckListBox usage 3 50
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now