Does anyone know a neat way to prevent users executing a script two or
more time simultaneously? Apart from teaching them to be more patient!
Ideally I'm looking for a way to make the script realise that it is
already running in a previous instance and either just quit or send the
user a message and then quit.
Cheers,
Danny....
--
Bill James
Microsoft MVP - Shell/User
Windows VBScript Utilities » www.billsway.com/vbspage/
Windows Tweaks & Tips » www.billsway.com/notes_public/
<Daniel...@gmail.com> wrote in message news:1157590548.2...@d34g2000cwd.googlegroups.com...
You could wrap your script in an HTA as it supports
SINGLEINSTANCE="yes"
Another advantage is that it allows a user interface.
Test as-is. Watch for word-wrap.
<html>
<head>
<title>test.hta</title>
<HTA:APPLICATION ID="HTA"
APPLICATIONNAME="TEST"
SYSMENU="no"
SCROLL="yes"
SINGLEINSTANCE="yes"
>
</head>
<script type="text/vbscript">
Option Explicit
Sub Clicked()
document.getElementById("Now").innerHTML = Now
End Sub
</script>
<body>
<form>
<input type="button" value="Click" onclick="Clicked()">
<input type="button" value="Close" onclick="self.close()">
</form>
<span id="Now"></span>
</body>
</html>
You could hold the file open exclusively and close at the end of the script.
If the script crashes the exclusive lock on the file will be released.
You could also check for a running script host, and then only run
your sub routine when other scripts have finished. This approach,
however, goes for all running scripts using wscript.exe or cscript.exe
(the function checks for only one or the other...whichever is passed as
a parameter) as its host. If this is the functionality you are looking
for give this code a shot.
The OtherScriptsRunning function accepts two parameters, a
computer name, and a script host name. The function returns TRUE if
there are other scripts running, and FALSE if not. To run your sub
routine only when other scripts are not running call it if the
condition is true.
(Please be careful of line wraps from posting.)
----------------------------
If (OtherScriptsRunning (".","wscript.exe")) = True Then
MyScript
Else
MsgBox "Please wait, other scripts are running"
End If
Function OtherScriptsRunning(strComputer,strScriptHost)
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")
OtherScriptsRunning = False
On Error Resume Next
Do While True
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strScriptHost
& "'")
If Err.Number = 0 Then
If colProcesses.Count = 0 Then
OtherScriptsRunning = True
Exit Function
End If
Else
MsgBox "Can't query host"
End If
Loop
End Function
Sub MyScript
MsgBox "No other scripts are running."
End Sub
-----------------------------
Hope this helps,
James Garringer
Danny....
For more info check out these links:
Introduction to HTML Applications (HTAs)
URL:http://msdn.microsoft.com/workshop/author/hta/overview/htaoverview.asp
HTA Developers Center
http://www.microsoft.com/technet/scriptcenter/hubs/htas.mspx
HTML Applications
http://www.htmlgoodies.com/beyond/reference/article.php/3472841