2015-11-21 38 views
0

我在使用一些參數啓動它之後異步讀取批處理文件的輸出。如果批處理文件正在等待輸入 - 輸入請求文本未被重定向 - 除非進程終止(顯然已不及時響應)。在.NET中異步讀取cmd輸出 - 懸掛進程輸入請求

如果在標準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 
+0

如果這是UNIX,你可能會編寫腳本爲'yes | prorest ...' –

+1

當收到全部行時觸發'OutputDataReceived'。但是,很明顯,你的批處理文件不會在''你想寫過它嗎? [y/n]:'線。 – PetSerAl

+0

湯姆 - 我這樣做的刪除(這將始終提示),但是,對於恢復它更多的是錯誤處理,因爲數據庫不應該存在,但我希望應用程序的選項通過接收提示安全地作出響應。 – madlan

回答

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(); 
}