异步读取.NET中的cmd输出 - 挂起进程输入请求

时间:2015-11-21 23:45:22

标签: .net cmd openedge progress-db

在使用某些参数启动后,我异步读取批处理文件的输出。如果批处理文件正在等待输入 - 输入请求文本没有被重定向 - 除非进程被终止(显然为时已晚,无法响应)。

如果在标准cmd窗口中执行,则提示符为:

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]:

使用重定向时的输出将挂起,而不会触发outputdatarecieved事件,因此我无法处理输入请求并做出相应的响应。控制台不读取最后一行(输入请求):

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

代码:

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

1 个答案:

答案 0 :(得分:1)

您可以编写自己的文本流阅读器例程,该例程将读取并报告不完整的行。这是一个简单的C#实现:

public static async Task ReadTextReaderAsync(TextReader reader, IProgress<string> progress) {
    char[] buffer = new char[1024];
    for(;;) {
        int count = await reader.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
        if(count==0) {
            break;
        }
        progress.Report(new string(buffer, 0, count));
    }
}

此实现只是从TextReader读取字符串并通过IProgress<string>实例报告它们。它不会通过换行符分割字符串,而是将它们保留在字符串中。还有一个测试程序:

public static void Main() {
    ProcessStartInfo psi = new ProcessStartInfo("Test.cmd") {
        UseShellExecute=false,
        RedirectStandardOutput=true,
        RedirectStandardError=true
    };
    Process p = Process.Start(psi);

    // Progress<T> implementation of IProgress<T> capture current SynchronizationContext,
    // so if you create Progress<T> instance in UI thread, then passed delegate
    // will be invoked in UI thread and you will be able to interact with UI elements.
    Progress<string> writeToConsole = new Progress<string>(Console.Write);

    Task stdout = ReadTextReaderAsync(p.StandardOutput, writeToConsole);
    Task stderr = ReadTextReaderAsync(p.StandardError, writeToConsole);

    // You possibly want asynchronous wait here, but for simplicity I will use synchronous wait.
    p.WaitForExit();
    stdout.Wait();
    stderr.Wait();
}