2011-06-19 59 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(); 
      } 
     } 

*/ 
相关问题