2008-09-15 27 views
18

Tôi có một đối tượng System.Diagnostics.Process trong một chương trình nhắm vào khuôn khổ Net 3.5Làm cách nào để biết khi nào OutputDataReceived cuối cùng đã đến?

tôi đã chuyển hướng cả StandardOutputStandardError Ống và tôi đang nhận dữ liệu từ họ không đồng bộ. Tôi cũng đã thiết lập một trình xử lý sự kiện cho sự kiện đã thoát.

Khi tôi gọi Process.Start() Tôi muốn tắt và thực hiện công việc khác trong khi chờ các sự kiện được nêu ra.

Thật không may có vẻ như, đối với một quá trình trả về một lượng lớn thông tin, sự kiện đã thoát sẽ được kích hoạt trước sự kiện OutputDataReceived cuối cùng.

Làm cách nào để biết khi nào OutputDataReceived cuối cùng đã được nhận? Lý tưởng nhất là tôi muốn sự kiện Exited là sự kiện cuối cùng tôi nhận được.

Dưới đây là một chương trình ví dụ:

using System; 
using System.Diagnostics; 
using System.Threading; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 

    static void Main(string[] args) 
    { 
     string command = "output.exe"; 
     string arguments = " whatever"; 

     ProcessStartInfo info = new ProcessStartInfo(command, arguments); 

     // Redirect the standard output of the process. 
     info.RedirectStandardOutput = true; 
     info.RedirectStandardError = true; 

     // Set UseShellExecute to false for redirection 
     info.UseShellExecute = false; 

     Process proc = new Process(); 
     proc.StartInfo = info; 
     proc.EnableRaisingEvents = true; 

     // Set our event handler to asynchronously read the sort output. 
     proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived); 
     proc.ErrorDataReceived += new DataReceivedEventHandler(proc_ErrorDataReceived); 
     proc.Exited += new EventHandler(proc_Exited); 

     proc.Start(); 
     // Start the asynchronous read of the sort output stream. Note this line! 
     proc.BeginOutputReadLine(); 
     proc.BeginErrorReadLine(); 

     proc.WaitForExit(); 

     Console.WriteLine("Exited (Main)"); 

    } 

    static void proc_Exited(object sender, EventArgs e) 
    { 

     Console.WriteLine("Exited (Event)"); 
    } 



    static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     Console.WriteLine("Error: {0}", e.Data); 
    } 



    static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     Console.WriteLine("Output data: {0}", e.Data); 
    } 


    } 
} 

Khi chạy chương trình này bạn sẽ nhận thấy rằng "thoát (Event)" xuất hiện ở một vị trí hoàn toàn biến trong đầu ra. Bạn có thể cần phải chạy nó một vài lần và, rõ ràng, bạn sẽ cần phải thay thế "output.exe" với một chương trình của sự lựa chọn của bạn mà tạo ra một số lượng phù hợp lớn của đầu ra.

Vì vậy, câu hỏi một lần nữa: Làm cách nào để biết khi nào OutputDataReceived cuối cùng đã được nhận? Lý tưởng nhất là tôi muốn sự kiện Exited là sự kiện cuối cùng tôi nhận được.

Trả lời

23

Câu trả lời cho điều này là e.Data will be set to null:

static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    if(e.Data == null) _exited.Set(); 
} 
Các vấn đề liên quan