User Tools

Site Tools


detecting_20a_20second_20instance_20of_20a_20program

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

detecting_20a_20second_20instance_20of_20a_20program [2018/03/31 13:19]
127.0.0.1 external edit
detecting_20a_20second_20instance_20of_20a_20program [2018/04/14 20:58] (current)
richardrussell Added syntax highlighting
Line 1: Line 1:
 =====Detecting a second instance of a program===== =====Detecting a second instance of a program=====
  
-//by Richard Russell, July 2006//\\ \\  There may be situations when you need to ensure that only one copy of a program can be run at a time; an attempt to run a second copy should fail (with or without an error message being displayed). There are several ways in which this can be achieved, but the one described here is relatively simple and free from unwanted side effects; it uses the Windows API.\\ \\  At the very start of your program add the following code:\\ \\ +//by Richard Russell, July 2006//\\ \\  There may be situations when you need to ensure that only one copy of a program can be run at a time; an attempt to run a second copy should fail (with or without an error message being displayed). There are several ways in which this can be achieved, but the one described here is relatively simple and free from unwanted side effects; it uses the Windows API.\\ \\  At the very start of your program add the following code: 
 + 
 +<code bb4w> ​
         SYS "​CreateMutex",​ 0, 1, "​UniqueLockName"​ TO Mutex%         SYS "​CreateMutex",​ 0, 1, "​UniqueLockName"​ TO Mutex%
         SYS "​GetLastError"​ TO lerr%         SYS "​GetLastError"​ TO lerr%
Line 8: Line 10:
           QUIT           QUIT
         ENDIF         ENDIF
-You should replace the string **UniqueLockName** with a name which is extremely unlikely to be chosen by another program. It can consist of up to 260 characters so you can incorporate your program'​s name, your name and other distinguishing features to achieve this (the only character not allowed is backslash). One method is to [[/​Creating%20a%20Globally%20Unique%20Identifier|generate a Globally Unique Identifier]],​ or GUID, to use as the unique lock name for your program. A GUID is a 128-bit pseudo-random number which is statistically guaranteed to be unique. The number of possible unique values is so large (2^128 or 3.4028×10^38) that the chance of the same value being generated twice is virtually zero.\\ \\  At the end of your program, just before it exits, add the following code:\\ \\ +</​code>​ 
 + 
 +You should replace the string **UniqueLockName** with a name which is extremely unlikely to be chosen by another program. It can consist of up to 260 characters so you can incorporate your program'​s name, your name and other distinguishing features to achieve this (the only character not allowed is backslash). One method is to [[/​Creating%20a%20Globally%20Unique%20Identifier|generate a Globally Unique Identifier]],​ or GUID, to use as the unique lock name for your program. A GUID is a 128-bit pseudo-random number which is statistically guaranteed to be unique. The number of possible unique values is so large (2^128 or 3.4028×10^38) that the chance of the same value being generated twice is virtually zero.\\ \\  At the end of your program, just before it exits, add the following code: 
 + 
 +<code bb4w>
         SYS "​ReleaseMutex",​ Mutex%         SYS "​ReleaseMutex",​ Mutex%
         SYS "​CloseHandle",​ Mutex%         SYS "​CloseHandle",​ Mutex%
-Ideally you should execute this code even if the program terminates abnormally, but in the event of a crash Windows should automatically delete the //mutex// for you.\\ \\  With the above code an attempt to run a second copy of a program will fail with no error message. If you want to alert the user to the problem you can display a message box by amending the code at the beginning of the program as follows:\\ \\ +</​code>​ 
 + 
 +Ideally you should execute this code even if the program terminates abnormally, but in the event of a crash Windows should automatically delete the //mutex// for you.\\ \\  With the above code an attempt to run a second copy of a program will fail with no error message. If you want to alert the user to the problem you can display a message box by amending the code at the beginning of the program as follows: 
 + 
 +<code bb4w>
         SYS "​CreateMutex",​ 0, 1, "​UniqueLockName"​ TO Mutex%         SYS "​CreateMutex",​ 0, 1, "​UniqueLockName"​ TO Mutex%
         SYS "​GetLastError"​ TO lerr%         SYS "​GetLastError"​ TO lerr%
Line 19: Line 29:
           QUIT           QUIT
         ENDIF         ENDIF
 +</​code>​
detecting_20a_20second_20instance_20of_20a_20program.txt · Last modified: 2018/04/14 20:58 by richardrussell