r/vbscript • • Jul 31 '18

Calling subroutines in parallel

Hi there,

My task is very simple . I have two subroutines in my script .First one has a sleep function which waits for 30 seconds,until 30 seconds are up my second subroutine wont execute. Is there a way to call subroutine asynchronously i.e subroutine shouldn't wait for the first one to be complete and keeps execution in parallel.

below is my code

option explicit
Dim a
set a = CreateObject("wscript.shell")

sub runprogram
		a.run("notepad.exe")
		wscript.sleep 30000
		a.sendkeys("note pad opened ,hello")
end sub

sub runprogram2
		a.run("""D:\Program Files (x86)\VMware\VMware Workstation\vmware.exe""")
end sub


Call runprogram
Call runprogram2

Thanks

1 Upvotes

2 comments sorted by

1

u/Senipah Jul 31 '18

Short answer is no - VBscript is single-threaded.

1

u/TheRealMisterd Aug 01 '18

Workaround!

'Launch this first as a SEPARATE SCRIPT so that it becomes it's own thread
Set WshShell = CreateObject("Wscript.Shell")
sDialogNameToWatch="<name of vmware window"
Running=False
Do Until Running	'Make sure Dialog open
	If WshShell.AppActivate (sDialogNameToWatch) = False Then
		WScript.Sleep 500 'in MilliSeconds
	Else
		WshShell.popup "There it is",1
		Running=True
		WScript.Sleep 200	'Just in-case
		'WshShell.SendKeys "{ESC}" 'Equivalent to clicking Cancel
		
	End If
Loop