1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Asynchronously read cmd output in .NET - Hanging on process input request

Discussão em 'StackOverflow' iniciado por fdantas, Novembro 21, 2015.

  1. fdantas

    fdantas Administrator Moderador

    I'm asynchronously reading the output from a batch file, after starting it with some parameters. If the batch file is waiting for input - the input request text is not being redirected - unless the process is terminated (which is obviously too late to respond).

    If executed in a standard cmd window, the prompt is:

    OpenEdge Release 10.2B07 as of Fri Sep 7 02:16:54 EDT 2012
    testdb already exists.
    Do you want to over write it? [y/n]:


    The output when using redirect will hang, without triggering the outputdatarecieved event, so I cannot process the input request and respond accordingly. The console does not read the last line (input request):

    OpenEdge Release 10.2B07 as of Fri Sep 7 02:16:54 EDT 2012
    testdb already exists.


    Code:

    Private Sub someMethod()
    Dim process As New Process()
    process.StartInfo = New ProcessStartInfo("C:\OEV10\bin\_dbutil")
    process.StartInfo.WorkingDirectory = "C:\Temp\"
    process.StartInfo.Arguments = "prorest testdb C:\Temp\testdb.bck -verbose"
    process.EnableRaisingEvents = True

    With process.StartInfo
    .UseShellExecute = False
    .RedirectStandardError = True
    .RedirectStandardOutput = True
    .RedirectStandardInput = True
    .CreateNoWindow = False
    .StandardOutputEncoding = System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo_OEMCodePage)
    .StandardErrorEncoding = System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo_OEMCodePage)
    End With

    AddHandler process.Exited, AddressOf ProcessExited
    AddHandler process.OutputDataReceived, AddressOf Async_Data_Received2
    AddHandler process.ErrorDataReceived, AddressOf Async_Data_Received2

    process.Start()
    process.BeginOutputReadLine()
    process.BeginErrorReadLine()
    End Sub

    Private Sub Async_Data_Received2(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
    Console.WriteLine(e.Data)
    End Sub

    Continue reading...

Compartilhe esta Página