Singleton (single instance or mutex) and process exist
Moderators: Dorian (MJT support), JRL
Singleton (single instance or mutex) and process exist
I would to allow only one instance of compiled macro scheduler exe running all the time and also checking process exist function.
Anyone have any ideas/method to do it?
Please share.
Anyone have any ideas/method to do it?
Please share.
When I want to limit an executable to one instance I create a dialog in the script. Be sure to give it a unique caption. I make sure the dialog is positioned well off the desktop or has an alphablend value of zero. Then the first lines of the same script tests to see if that unique window exists. If the window exists the script exits immediately.
Something like this:
Not sure I understand the second part of your question. Do you want to control the single instance script to make sure it is always running?
Something like this:
Code: Select all
IfWindowOpen>ThisWindowBelongsToMyScript.exe
MDL>Program already running
Exit>0
EndIf
OnEvent>Key_down,VK27,0,Quit
Dialog>Dialog1
object Dialog1: TForm
AlphaBlend = True
AlphaBlendValue = 0
Caption = 'ThisWindowBelongsToMyScript.exe'
end
EndDialog>Dialog1
Label>Loop
Wait>0.01
Goto>Loop
SRT>Quit
Exit>0
END>Quit
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Just use the ProcessExists function. Check for existence of self exe.
Or use a flag in an ini file, or registry entry, or create a text file, check existence of. All kinds of ways.
Or use a flag in an ini file, or registry entry, or create a text file, check existence of. All kinds of ways.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Not sure how that works since as soon as you start the program the process exists so testing for "self" always tests true.Just use the ProcessExists function. Check for existence of self exe.
I had to look up "mutex" and on a Microsoft web site they mention the same things. Anyway they say use a file which is essentially what you are saying.
I like using a window because when the script is gone the window is gone no matter how the script died. Using a file has risk in that the script or the computer could crash and the file gets left behind preventing the program from running the next time it is started. Yes, steps can be taken to deal with that possibility but those steps must be thought out and added to the script. So though the file method seems an easy solution, making it work flawlessly takes a little effort. The window method just works.
-
- Automation Wizard
- Posts: 1101
- Joined: Fri Jan 07, 2005 5:55 pm
- Location: Somewhere else on the planet
Maybe you could use VBScript IsProcessRunning at the start of the exe, if it returns >1 terminate, otherwise continue.
Code: Select all
VBSTART
'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name='" & ProcessName & "'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("excel.exe"),res
//If>res<2 do whatever, else quit
Yes, I've also used that script first posted by Marcus back near the beginning of MS time. I think I came up with the dialog method and started using it because it doesn't require looking anything up. It also actually has fewer lines. What I posted earlier can be shortened to five essential lines
Then the rest of your script. Put these five lines at the top of the script and only one instance of the program will be allowed.
Code: Select all
IfWindowOpen>ThisWindowBelongsToMyScript
Exit>0
EndIf
Dialog>ThisWindowBelongsToMyScript
EndDialog>ThisWindowBelongsToMyScript
Me_again wrote:Does the dialog die if the program crashes?
JRL wrote:I like using a window because when the script is gone the window is gone no matter how the script died. Using a file has risk in that the script or the computer could crash and the file gets left behind preventing the program from running the next time it is started. Yes, steps can be taken to deal with that possibility but those steps must be thought out and added to the script. So though the file method seems an easy solution, making it work flawlessly takes a little effort. The window method just works.
Your 5 lines of code work perfectly for the past month, thank you. Will continue to use this method for all my singleton app.JRL wrote:Yes, I've also used that script first posted by Marcus back near the beginning of MS time. I think I came up with the dialog method and started using it because it doesn't require looking anything up. It also actually has fewer lines. What I posted earlier can be shortened to five essential linesThen the rest of your script. Put these five lines at the top of the script and only one instance of the program will be allowed.Code: Select all
IfWindowOpen>ThisWindowBelongsToMyScript Exit>0 EndIf Dialog>ThisWindowBelongsToMyScript EndDialog>ThisWindowBelongsToMyScript