Mega Code Archive

 
Categories / Delphi / Examples
 

One instance application professional way

Title: one instance application - professional way Question: how can I have only one instance of my application running ? Answer: add this code to your application unit: .. var mutex : THandle; mutexName : array [0..7] of char; begin try StrPCopy(mutexName ,'anyname'); mutex := openmutex ( MUTEX_ALL_ACCESS, False, mutexName ) ; If mutex = 0 then begin mutex := createmutex ( nil , true, mutexName); Application.Initialize; Application.CreateForm ( TForm1, Form1 ); Application.Run; ReleaseMutex (mutex); end else begin Messagedlg( 'The application is already running.' , mtinformation, [mbOk],0 ); Application.Terminate; end; finally ... end; end. Please see the windows SDK help for more information about "mutex"