Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run Only One Instance - Exit If Already Running..??

Status
Not open for further replies.

GUMBAZ

Technical User
Sep 12, 2007
9
US
I'm new to VBS and in need of some code that will allow only once instance of the VBS File to be ran at a time..
so if the 1st instance is already running, a 2nd instance cannot be ran...

Is this possible at all or not..??
If so How do i code it..??
 
This should be possible using WMI and the Win32_Process class on WinXP or 2k3.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
C

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
The C was a test, the site was having issues letting me post a reply. Trying again.

Set objWMIServie = GetObject("winmgmts:\\.\root\cimv2")
strQuery = "Select * from Win32_Process"
Set ColItems = objWMIService.ExecQuery(strQuery,,48)

For Each objItem in colItems
msgbox objItem.name
Next

There's plenty more to be found using Dm's suggestion of WMI and Win32_Process Class if your willing to look. Hope this helps.

[thumbsup2] Wow, I'm having amnesia and deja vu at the same time.
I think I've forgotten this before.


 
Like MrMilson has shown...a property you will be interested in looking is "CommandLine" which as previously stated is only available on WinXP or greater.

See:
--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
sorry its not working for me..!!
I'm getting this error:
66xh090.png


this is my VBS code:
its suppose to start calc.exe and wait for a while so i can try an run a 2nd instance of the 1.vbs file, and if its already running it ends itself--- I hope...
whats wrong though- why am i getting that ERROR..??
------------------------------------------------
Set objWMIServie = GetObject("winmgmts:\\.\root\cimv2")
strQuery = "Select * from Win32_Process"
set ColItems = objWMIService.ExecQuery(strQuery,,48)

For Each objItem in colItems
msgbox objItem.name
Next

Set objApp = CreateObject("WScript.Shell")
objApp.run "CALC.EXE"

wscript.sleep 500
 
Set objWMIServi[!]c[/!]e = GetObject("winmgmts:\\.\root\cimv2")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
ok i just tried it in my VMWARE PC and i got no errors this time..
BUT.. all it does is just list all the currnetly running processes and then starts calc..

i made the wscript.sleep longer to 90000 so after calc was started I could try and run the script again.. But i was still able to run the 1.vbs again..

i thought this script was to prevent that from happening.. whats wrong..??
 
>i made the wscript.sleep longer to 90000 so after calc was started I could try and run the script again.. But i was still able to run the 1.vbs again..
What makes you think it will prevent anybody from running 1.vbs again and again and again at any time?
 
well thats what im asking. how can i make the 1.vbs to be able to run in only one instance of itself..??

what im trying to do is compile the 1.vbs into an EXE with this ExeScript 3.0 and the 1.vbs is just a simple code to start another EXE within the resource compartment of this compiled 1.vbs-exe and then it waits for like 1-min. so the 1.vbs-exe cant be ran in more than one instance of itself at the same time but for only like a 1-min.

thats why im asking for some code here, so i can prevent the 1.vbs-exe from running in more than one instance of itself..
 
>well thats what im asking. how can i make the 1.vbs to be able to run in only one instance of itself..??
well, it didn't sound like you were asking as a response to somebody's advice.

To response to your asking, add this script block at the top of your existing script. This script is based on the commandline property of win32_process instances. Hence, it is supported only for winxp up.
[tt]
'Functionality supported only for winXP up
dim svc, squery, ncount
set svc=getobject("winmgmts:root\cimv2")
squery="select commandline from win32_process " & _
"where (not ((commandline like ""%command.com%"") or (commandline like ""%cmd.exe%""))) and " & _
"(commandline like ""%[WC]Script.exe%" & wscript.scriptname & "%"")"
ncount=svc.execquery(squery).count
set svc=nothing
if ncount>1 then
wscript.quit
end if
'continue with the existing script
[/tt]
 
thanks, that code seems to help,
but when I compile this code with the ExeScript 3.0 it doesn't work anymore..
is that because this code is only looking for wscript.exe and when my code is compiled into an exe an ran it doesn't use wscript.exe...??

How would I change that code so it will work when i compile it into an EXE with the ExeScript 3.0..???
 
I have no intention to try that commercial software (which said free to try without detail on the condition or expiry info).

In any case, if you have it "compile" to exe, say abc.exe, from the script named abc.vbs, I would suggest you try to change the query string from the above to this for testing.
[tt]
squery="select commandline from win32_process " & _
"where (not ((commandline like ""%command.com%"") or (commandline like ""%cmd.exe%""))) and " & _
"(commandline like [blue]""%" & replace(wscript.scriptname,".vbs",".exe") & "%""[/blue])"
[/tt]
(of couse you can fabricate case to break the simple use of replace like calling the vbs abc.exe.vbs of similar names, but that is just a guard, very artificial name. Just try to avoid those as a scripter has control on the name of the script name.)

From a broader point of view, I would suggest the vbs should not just be a wrapper spawning other process (vbs... or exe, com) to do some job. In that case, the application abc.exe would surround the control after spawning those processes and diminishing largely the usefulness of checking of the sort.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top