INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Call Subroutine Asynchronously

Call Subroutine Asynchronously

(OP)
I have the following code that creates a process and then continually checks to make sure it's still running.
If it finds that it is closed, it creates it again and then loops forever.

CODE --> vbscript

Set objWMIServiceWin32Process = GetObject("WinMgmts:\\.\root\cimv2:Win32_Process")
	objWMIServiceWin32Process.Create strCmd1, null, null, intID1
	objWMIServiceWin32Process.Create strCmd2, null, null, intID2

Set objWMIServiceRoot = GetObject("WinMgmts:\\.\root\cimv2")
	Set colProcesses = objWMIServiceRoot.ExecNotificationQuery("Select * From __InstanceDeletionEvent Within 1 Where TargetInstance ISA 'Win32_Process'")
Do
	Set objProcess = colProcesses.NextEvent
	Select Case objProcess.TargetInstance.ProcessID
		Case intID1
			objWMIServiceWin32Process.Create strCmd1, null, null, intID1
		Case intID2
			objWMIServiceWin32Process.Create strCmd2, null, null, intID2
	End Select
Loop 

My question is, can I have that loop run asynchronously so that I can run a bunch of other code in another loop.

Currently, I accomplish this by having two separate scripts and using Windows Task Scheduler to run them.
This set of code runs continually as fast as it can but the other code, I want it to run every 5 minutes.
I could accomplish this with a loop running every 5 minutes but I don't want to slow down this set of code.
I am just trying to combine the two scripts into one so I can lower the deployment overhead of this program.

Any suggestions?
Thanks!

RE: Call Subroutine Asynchronously

Sure, you just need an event sink, e.g.

CODE

Dim EventFired
strCmd1="notepad.exe"
strCmd2="calc.exe"

Set objWMIServiceWin32Process = GetObject("WinMgmts:\\.\root\cimv2:Win32_Process")
objWMIServiceWin32Process.Create strCmd1, null, null, intID1
objWMIServiceWin32Process.Create strCmd2, null, null, intID2
	
Set objWMIServiceRoot = GetObject("WinMgmts:\\.\root\cimv2")
Set objSink = WScript.CreateObject("WbemScripting.SWbemSink","objSink_")

' Async version
objWMIServiceRoot.ExecNotificationQueryAsync objSink, "Select * From __InstanceDeletionEvent within 1 Where TargetInstance ISA 'Win32_Process'"

' This loop represents your code doing something else whilst waiting for events
Do Until EventFired
    WScript.Sleep 500
Loop

' Event ...
Sub objSink_OnObjectReady(objLatestEvent, objWMIAsyncContext)
    Select Case objLatestEvent.TargetInstance.ProcessID
        Case intID1
            objWMIServiceWin32Process.Create strCmd1, null, null, intID1
            EventFired=true ' for puposes of this example, want script to terminate after event fires
        Case intID2
            objWMIServiceWin32Process.Create strCmd2, null, null, intID2
            EventFired=True ' for puposes of this example, want script to terminate after event fires
    End Select
End Sub  

RE: Call Subroutine Asynchronously

(OP)
Thanks for the code.
From looking this over and then trying it out real quick, I believe this allows it to run async, but it's only executed once.
I'm basically trying to execute two things at the same time and loop them forever.
One loops as fast as it can, the other loops every 5 minutes.

RE: Call Subroutine Asynchronously

(OP)
UPDATE:
Please disregard that last comment.
I had some typos when I adapted it to my application.
This code works exactly as I need.
You are the best, thank you.

RE: Call Subroutine Asynchronously

(OP)
Actually, now that I'm trying to put this into production, the script is dying.
Seems that the unprivileged user I'm running this script as dies at this statement:

CODE --> vbscript

objWMIServiceRoot.ExecNotificationQueryAsync objSink, "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process'" 

Since I'm running this via Task Scheduler as an unprivileged user, I can only see that the process (wscript) is still running but I find out where it's stuck by placing the following at various parts of the file:

CODE --> vbscript

objFSO.CreateTextFile(strDirRoot & "HERE") 

Using that technique I'm able to gather the following:
-2147217400
Invalid parameter

This works fine as an administrator

Any suggestions?

RE: Call Subroutine Asynchronously

Suffice it to say that running my code as an unprivileged user from the scheduler works exactly as I'd expect (always remembering that, unless running as the logged in user, the task cannot interact with the desktop - which I think you already know). i.e. I can't replicate your issue, I'm afraid

RE: Call Subroutine Asynchronously

(OP)
Ok,
This is on a domain controller so looks like I'll have to start digging through Group Policy: "Local Policies/User Rights Assignment"
I had to add the user to: "Log on as a batch job" to get this to run initially.
I wonder if more is needed.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close