Singleton (single instance or mutex) and process exist

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
newuser
Pro Scripter
Posts: 64
Joined: Tue Jun 11, 2013 4:53 pm

Singleton (single instance or mutex) and process exist

Post by newuser » Tue Jun 11, 2013 5:09 pm

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.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jun 11, 2013 5:29 pm

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:

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
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?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jun 11, 2013 9:41 pm

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.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Jun 11, 2013 10:19 pm

Just use the ProcessExists function. Check for existence of self exe.
Not sure how that works since as soon as you start the program the process exists so testing for "self" always tests true.

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.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Wed Jun 12, 2013 1:46 am

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

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Jun 12, 2013 3:41 am

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

Code: Select all

IfWindowOpen>ThisWindowBelongsToMyScript
  Exit>0
EndIf

Dialog>ThisWindowBelongsToMyScript
EndDialog>ThisWindowBelongsToMyScript
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.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Wed Jun 12, 2013 1:59 pm

Does the dialog die if the program crashes? What I don't like about the file method is that the file may be left behind if the program stalls/crashes and the program will never restart.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Jun 12, 2013 2:19 pm

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.

newuser
Pro Scripter
Posts: 64
Joined: Tue Jun 11, 2013 4:53 pm

Post by newuser » Sat Jul 13, 2013 5:12 pm

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 lines

Code: Select all

IfWindowOpen>ThisWindowBelongsToMyScript
  Exit>0
EndIf

Dialog>ThisWindowBelongsToMyScript
EndDialog>ThisWindowBelongsToMyScript
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.
Your 5 lines of code work perfectly for the past month, thank you. Will continue to use this method for all my singleton app. :D

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts