Important alert: (current site time 7/16/2013 5:51:50 AM EDT)
 

VB icon

Article: RunOnlyOnce: A short discussion; FIndWindow versus GlobalAtoms versus Mutex.

Email
Submitted on: 12/19/2001 9:52:31 AM
By: David Ward 
Level: Beginner
User Rating: Unrated
Compatibility: Delphi 5, Delphi 4, Pre Delphi 4
Views: 10212
 
     Article: RunOnlyOnce: A short discussion; FIndWindow versus GlobalAtoms versus Mutex.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
!**************************************
! Name: Article: RunOnlyOnce: A short discussion; FIndWindow versus GlobalAtoms versus Mutex.
! Description:Article: RunOnlyOnce: A short discussion; FIndWindow versus GlobalAtoms versus Mutex.
! By: David Ward
!
!This code is copyrighted and has! limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=535&lngWId=7!for details.!**************************************

When I was first learning to create Windows programs, I used;
If FindWindow(pchar('TForm'),pchar('Title_of_my_Form'))<>0 Halt;
Crude, but it works as long as, [1] You do not mind any initialisation code you may have in the "OnCreate"
event being executed twice (once for the instance that may be already running and again for subsequent
execution attempts), [2] Your application does actually have a "Form", i.e. Is not a console or background app
and [3] You can live with the irritating flash of the main form showing and then immediately closing.
So, I progressed to the "GlobalAtom" method (I have recently seen the code example of this posted by "Mr
Jerome Scott II", it is absolutely perfect and should work just fine) this method does not suffer from the 
shortfalls as described for FindWindow, but, you have to destroy the Atom on program termination and should 
your program crash (as they sometimes do during initial testing and debugging) the Atom remains in-situ, very 
irritating when you expect to make minor mods and rerun it almost immediately. There are ways round that for 
debugging purposes of course, but they defeat the object of the exercise for a program when it goes on 
general release. Most users of your software will not want to reboot their machine to rerun a program that has 
already crashed and then refuses to run again because it thinks it is still running. All that said, it is a fine 
starting point as a solution for this problem.
Finally, I have settled on the Mutex method. You, the programmer, do not have to destroy any MutexObjects 
that you have created, Windows will do this for you when your program terminates (or crashes!). Now then, 
whilst MutexObjects were not specifically designed for this particular purpose (you can always read up on the 
other, advanced, uses they can be put to), I have found them to be the cleanest effective method of dealing 
with RunOnlyOnce issues.
You will find my code solution for this very close in "the list" to this article


Other 4 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.