What I am trying to do:
There are three powershell scripts with different time delays as shown below. I am trying to run them asynchronously in .NET and I followed this article to implement Asyncrhonous programming.
Where I am stuck:
The I am not able to retrieve output after the events are invoked. The scripts are being called but then the program ends and it shows “Press any key to continue” in console windows. I don’t what I am missing here. Any idea where I am doing wrong?
Info: JobRequest is a class that I use to pass around information keep track of jobs.
If you find a simpler and easy to understand explanation about Async programming then please provide it as all the article are confusing and long.
Sub Main()
OurAsyncFunctionCalling("psDelayScript2.ps1", "-arg1 4 -arg2 5", 1)
OurAsyncFunctionCalling("psDelayScript1.ps1", "-arg1 2 -arg2 3", 2)
OurAsyncFunctionCalling("psDelayScript.ps1", "-arg1 1 -arg2 1", 3)
End Sub
Delegate Function AsyncMethodHandler(ByVal ScriptFile As String, ByVal ScriptParameters As String, ByVal id As String) As JobRequest
Public Function RunScript(ByVal ScriptFile As String, ByVal ScriptParameters As String, ByVal id As String) As JobRequest
Dim job1 As New JobRequest
Dim start As New ProcessStartInfo
Dim ScriptsFolder As String = "C:\BadTempScripts"
' start.FileName = "C:\WINDOWS\system32\cscript.exe"
start.FileName = "powershell.exe"
start.Arguments = ScriptsFolder + "\" + ScriptFile + " " + ScriptParameters
start.UseShellExecute = False
start.RedirectStandardOutput = True
start.RedirectStandardError = True
Dim myproc As New Process
myproc.StartInfo = start
myproc.Start()
Dim so As System.IO.StreamReader
Dim se As System.IO.StreamReader
se = myproc.StandardError
so = myproc.StandardOutput
myproc.WaitForExit()
job1.StandardError = so.ReadToEnd
job1.StandardOutput = so.ReadToEnd
Return job1
End Function
Public Sub OurAsyncFunctionCalling(ByVal strfile As String, ByVal strparameter As String, ByVal intid As Integer)
Dim caller As AsyncMethodHandler
caller = New AsyncMethodHandler(AddressOf RunScript)
caller.BeginInvoke(strfile, strparameter, intid, AddressOf Callbackmethod, Nothing)
End Sub
Private Sub Callbackmethod(ByVal ar As IAsyncResult)
Try
Dim result As AsyncResult = CType(ar, AsyncResult)
Dim caller As AsyncMethodHandler = CType(result.AsyncDelegate, AsyncMethodHandler)
Dim returnvalue As JobRequest = caller.EndInvoke(ar)
UpdateProcessCompleteLogic(returnvalue)
Catch ex As Exception
End Try
End Sub
Delegate Sub UpdateProcessCompleteHandler(ByVal jr As JobRequest)
Public Sub UpdateProcessCompleteLogic(ByVal jr As JobRequest)
Dim updatehandler As New UpdateProcessCompleteHandler(AddressOf updateprocess)
Dim results As JobRequest = jr
End Sub
Sub updateprocess(ByVal jr As JobRequest)
Console.WriteLine(jr.StandardError)
Console.WriteLine(jr.StandardOutput)
End Sub
I have found the solution to this mistake. In my asynchronous function, after I do the begin invoke call I ended the routine and the subroutine which was called asynchronously didn’t had any parent thread where it could report that the call was completed. The main thread should be running in order to see the results returned from the async callback function.