2011-06-19 58 views
4

想知道你能幫我嗎?啓動並觀看終止應用程序? c#

我有一個應用程序(app1),可以啓動另一個應用程序(app2),而app2正在運行app1能夠對app2生命週期中更改的文件夾/文件進行一些修改。我可以做到這一點,並運行完美。

但是我想知道是否有任何方法可以監控app2,看它何時被終止,以便我可以告訴app1一個人停止跟蹤?

我沒有app2的源代碼,所以我無法編輯它向app1發送信號。

感謝您的時間。

馬特

+1

看看[此線索](http://stackoverflow.com/questions/470256/process-waitforexit-asynchronously/470288) –

回答

2

如果您正在使用Process使用Process.Exited event

考慮這個事件只能出現如果EnableRaisingEvents屬性爲true

編輯:如果您需要app2的輸出@伊萬的回答是更好的,你需要等待出口。

+0

如果您使用OutputDataReceived和ErrorDataReceived處理程序,則可以使用此方法獲取輸出。 –

2

你可以那樣做:

var p = Process.Start("app2.exe"); 
// do start watching here 
p.WaitForExit(); 
// do stop watching here 

注意,它不是生產質量的代碼,因爲如果APP 2將掛起 - 這個代碼將等待它永遠。有超載可以指定超時,因此您可以事先完成等待。

或者按照Navid的建議使用Process.Exited事件。

+0

這看起來像一個很好的soloution我可以問,雖然,有什麼了好處這有在Navid的建議?(和反之亦然?)謝謝 –

+0

那麼,等待一個'WaitForExit'進程保持你的線程忙,所以你不能做任何事情。如果你看着異步的性質,比如獲取操作系統在線程池線程上調用的通知 - 這沒關係。相反,如果你有一些用戶界面,並且不能接受它的線程(因爲在用戶界面線程繁忙的時候用戶界面將會是'沒有響應') - 事件更適合。 –

+0

這是同步和直接的。另一種方法釋放並不會在你的應用程序運行時綁定你的應用程序。 –

0

這是我在幾個沒有任何問題的項目上使用過的(到目前爲止)。

using System.Diagnostics; 
using System; 

namespace [whatever] 
{ 
    class Executor { 
     public event EventHandler Completed; 
     public event DataReceivedEventHandler DataReceived; 
     public event DataReceivedEventHandler ErrorReceived; 

     public void callExecutable(string executable, string args, string workingDir){ 
      string commandLine = executable; 
      ProcessStartInfo psi = new ProcessStartInfo(commandLine); 
      psi.UseShellExecute = false; 
      psi.LoadUserProfile = false; 
      psi.RedirectStandardOutput = true; 
      psi.RedirectStandardError = true; 
      psi.WindowStyle = ProcessWindowStyle.Minimized; 
      psi.CreateNoWindow = true; 
      psi.Arguments = args; 
      psi.WorkingDirectory = System.IO.Path.GetDirectoryName(executable); 
      psi.WorkingDirectory = workingDir; 
      Process p = new Process(); 
      p.StartInfo = psi; 
      try{ 
       p.EnableRaisingEvents = true; 
       p.Start(); 
       if (DataReceived != null) p.OutputDataReceived += DataReceived; 
       if (ErrorReceived != null) p.ErrorDataReceived += ErrorReceived; 
       p.BeginOutputReadLine(); 
       p.BeginErrorReadLine(); 
       p.Exited += new EventHandler(p_Exited); 
      } 
      catch (Exception ex){ 
       //log.Error(ex.Message); 
      } 
     } 

     void p_Exited(object sender, EventArgs e) { 
      (sender as Process).Close(); 
      (sender as Process).Dispose(); 
      if (Completed != null) { 
       Completed(this, e); 
      } 
     } 
    } 
} 
/* 
    //In another class 
     private void CallProgram() { 
      Executor exec = new Executor(); 
      exec.Completed += new EventHandler(exec_Completed); 
      exec.DataReceived += new System.Diagnostics.DataReceivedEventHandler(exec_DataReceived); 
      exec.ErrorReceived += new System.Diagnostics.DataReceivedEventHandler(exec_ErrorReceived); 
      exec.callExecutable([Program Name], 
       [Arguments], 
       [Working Dir]); 
     } 

     void exec_Completed(object sender, EventArgs e) { 
      MessageBox.Show("Finshed"); 
     } 

     void exec_DataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) { 
      if (e.Data != null) { 
       AddText(e.Data.ToString()); 
      } 
     } 

     void exec_ErrorReceived(object sender, System.Diagnostics.DataReceivedEventArgs e) { 
      if (e.Data != null) { 
       AddText(e.Data.ToString()); 
      } 
     } 

     // this handles the cross-thread updating of a winforms control 
     private void AddText(string textToAdd) { 
      if (textBox1.InvokeRequired) { 
       BeginInvoke((MethodInvoker)delegate() { AddText(textToAdd); }); 
      } 
      else { 
       textBox1.AppendText(textToAdd); 
       textBox1.AppendText("\r\n"); 
       textBox1.Refresh(); 
      } 
     } 

*/ 
相關問題