2008-09-15 14 views
18

.NET framework 3.5Son OutputDataReceived'in ne zaman geldiğini nasıl bilebilirim?

ben hem StandardOutput ve StandardError boruları yönlendirildi ve ben uyumsuz onlardan veri alıyorum hedefleyen programda bir System.Diagnostics.Process nesne var. Ayrıca, Çıkış etkinliği için bir olay işleyici ayarlıyorum.

Bir kez Process.Start() numaralı telefonu arayacağım Olayları kaldırmak için beklerken diğer işlere devam etmek istiyorum.

Maalesef, büyük miktarda bilgi döndüren bir işlem için, Çıkış olayı son OutputDataReceived olayından önce tetiklenir.

Son OutputDataReceived'un ne zaman alındığını nasıl öğrenebilirim? İdeal olarak, aldığım son olay olan Exited olayının olmasını isterim. İşte

bir örnek programıdır:

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


    } 
} 

siz "Exited (Olay)" çıkış içinde tamamen değişken bir konumda göründüğünü fark edeceksiniz bu program çalışıyor. Bunu birkaç kez çalıştırmanız gerekebilir ve açık bir şekilde, "output.exe" yi, tercih ettiğiniz bir programla uygun bir şekilde büyük miktarda çıktı üretmeniz gerekecektir.

Yani, tekrar soru: Son OutputDataReceived'un ne zaman alındığını nasıl bilebilirim? İdeal olarak, aldığım son olay olan Exited olayının olmasını isterim.

cevap

23

bu cevabı e.Data will be set to null o:

static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e) 
{ 
    if(e.Data == null) _exited.Set(); 
} 
İlgili konular