Script Center >
Scripting Forums
>
The Official Scripting Guys Forum!
>
Setting up multiple event monitoring procedures in a single VB Script file
Setting up multiple event monitoring procedures in a single VB Script file
- I have a need to setup __InstanceCreation, __InstanceModification, and System Shutdown events in a single VB Script file that has to run at startup. I have created 3 different SUB's for each of these events in a vb script file. When I am trying to RUN the script the first SUB only gets executed and waits for an event to happen. The second and third SUB are not executed at all. How can I come out of a SUB after setting up monitoring to continue execution of the script.....allowing to set up next minitoring event or start executing the next statement.
Answers
- Yes, as long as the called script is configured properly. That line does launch a new thread, that is separate from the calling routine. However, all of the target subroutines must be represented as instrumented Case branches based on the passed argument, if they are all in one script. The approach could use a different script for each subroutine, addressing each in turn. However, I find having everything in a single script to be better for maintenance purposes, even though the script is more complicated.
The example did this by invoking a new instance of the the original script. The important feature of the Exec function is that it returns an object that connects the calling routine to the called thread. That is what the example was attempting to show.
Hope that answers your question.
Tom Lavedas- Marked As Answer byKiranChaturvedula Friday, November 06, 2009 6:53 AM
All Replies
- Try the wshShell.Exec method to launch your monitoring events separately from within the individual SUBs.
Also, shutdown events cannot happen during the computer startup script, they are blocked. Check out this posting on how to get around that issue.
How to schedule a computer reboot in the computer startup script
http://networkadminkb.com/kb/Knowledge%20Base/Windows2003/How%20to%20schedule%20a%20computer%20reboot%20in%20the%20computer%20startup%20script.aspx- Marked As Answer byKiranChaturvedula Tuesday, November 03, 2009 3:04 PM
- Unmarked As Answer byKiranChaturvedula Tuesday, November 03, 2009 3:11 PM
- Did you mean I can call a SUB using WshShell.Exec("nameOfSUB") in a script.
I tried the above method as below:
chkNetworkStatus( )
WshShell.Exec("flagNetworkStatusModificationEvent") 'the one in quotes is the SUB that creates a monitoring event on network status
chkDOORSExportFileExistence( )
But the script does not execute the statement "chkDOORSExportFileExistence()" at all.
Ideally, I would like the control to be transferred back from the monitoring SUB to the next statement. - I don't believe Gunner999 meant that you can call the SUB in EXEC. At least, if he did, he is wrong. Rather, each sub must be executed as a stand-alone script, where each is launched in turn and then tracked in a wait loop to check whether an event has triggered. To keep from requiring seperate script files, a reentry arrangement based on passed command line arguments could be used, something like this ...
set oWS = createobject("wscript.shell")
select case wsh.arguments
case .Item(0) = "Sub1" ' execute sub 1
flagNetworkStatusModificationEvent arguments if any
wsh.exit
case .Item(0) = "Sub2"
subroutine2Name arguments
wsh.exit
case .Item(0) = "Sub3"
subroutine2Name arguments
wsh.exit
case Else ' first time into script
' Create asynchronous calls to the subroutines - each creates a console window
set oSub1 = oWS.Exec("cscript " & wsh.scriptfullname & " Sub1")
set oSub2 = oWS.Exec("cscript " & wsh.scriptfullname & " Sub2")
set oSub3 = oWS.Exec("cscript " & wsh.scriptfullname & " Sub3")
' Check for event and wait in it has not happened
b1Happened = False
b2Happened = False
b3Happened = False
do
if oSub1.Status <> 0 and not b1Happened then
' do something because event 1 has happened
b1Happened = true
end if
if oSub2.Status <> 0 then
' do something because event 2 has happened
b2Happened = true
end if
if oSub3.Status <> 0 then
' do something because event 3 has happened
b3Happened = true
end if
wsh.sleep 100 ' release CPU for 100 ms to wait for event
loop until oSub1.Status <> 0 and oSub2 <> 0 and oSub3 <> 0 ' all three events have fired
end select
The only thing the three subroutines would do is wait for the event and then exit the script.
BTW, this is "air code" to illustrate the concept. I'm sure there is a lot more that needs to be worked out as this approach is coded and tested.
Tom Lavedas - Appreciate your reply ....
I would like to know about the below statement
set oSub1 = oWS.Exec("cscript " & wsh.scriptfullname & " Sub1")
does this mean that I can call any sub in a script using this statement and it will fork out as a separate process? - Yes, as long as the called script is configured properly. That line does launch a new thread, that is separate from the calling routine. However, all of the target subroutines must be represented as instrumented Case branches based on the passed argument, if they are all in one script. The approach could use a different script for each subroutine, addressing each in turn. However, I find having everything in a single script to be better for maintenance purposes, even though the script is more complicated.
The example did this by invoking a new instance of the the original script. The important feature of the Exec function is that it returns an object that connects the calling routine to the called thread. That is what the example was attempting to show.
Hope that answers your question.
Tom Lavedas- Marked As Answer byKiranChaturvedula Friday, November 06, 2009 6:53 AM
- Yes!. I got it. This was better than what I expected. Now, I understand the idea behind your example. It was good to know.
Thank you,
Kiran Chaturvedula,
Lead Professional-Requirements Management,
GE INFRA, AVIATION.

