locked
Problem with with output redirection of a process RRS feed

  • Question

  • User-1522632901 posted

    Hi!

    Maybe you peeps can help me. I am doing a program that runs diverse powershell scripts from the interface...

    It works! Cool    once.Yell

    Every time I try and start another script, VB throws me an "invalidOperationException" saying that "An async read operation has already been started on the stream".

    I do not get it, as I do close the process after all is done to redo it anew. What am I missing?

    Here is the code that calls the script

    Public Sub RunScript(Script As String, Param1 As String, Param2 As String)
    
            myProcess.StartInfo.FileName = "powershell"
            myProcess.StartInfo.Arguments =  
    myProcess.StartInfo.UseShellExecute = False myProcess.StartInfo.CreateNoWindow = True myProcess.StartInfo.RedirectStandardInput = False myProcess.StartInfo.RedirectStandardOutput = True myProcess.StartInfo.RedirectStandardError = False myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentCulture.TextInfo.OEMCodePage) myProcess.EnableRaisingEvents = True Application.DoEvents() AddHandler myProcess.OutputDataReceived, AddressOf proc_OutputDataReceived myProcess.Start() myProcess.BeginOutputReadLine() myProcess.Close() End Sub

    I had to remove my arguments, as the line was screwing the code renderer here...

    The error pops on the "myProcess.BeginOutputReadLine()" line.

    Thanks for the help!

    Friday, September 23, 2016 2:49 PM

Answers

  • User2053451246 posted

    Application.DoEvents()

    AddHandler myProcess.OutputDataReceived, AddressOf proc_OutputDataReceived

    What is going on in these events?  Any stream reading/writing?  Or anything else that would help us figure out where the problem is?  If you comment out those lines and test do you have the same problem?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2016 3:04 PM

All replies

  • User2053451246 posted

    Application.DoEvents()

    AddHandler myProcess.OutputDataReceived, AddressOf proc_OutputDataReceived

    What is going on in these events?  Any stream reading/writing?  Or anything else that would help us figure out where the problem is?  If you comment out those lines and test do you have the same problem?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2016 3:04 PM
  • User-1522632901 posted

    Hi!

    Thanks for the reply. The DoEvents was pasted from the code I found.  it passes control from the application to the operating system.  It helps in updating in real time, without dabling in threaded porcesses.

    the AddHandler line is for redirecting the powershell ofput to a text box. Here is the code:

        Public Sub proc_OutputDataReceived(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
            If Me.InvokeRequired = True Then
                Me.Invoke(myDelegate, e.Data)
            Else
                UpdateTextBox(e.Data)
    
            End If
        End Sub

    It is necessary.

    But I did try and remove the do events and lo and behold, it works now!

    So mark this as solved and a big thanks. I will be more wary of that function from now on...

    Friday, September 23, 2016 3:31 PM