启动和查看终止应用程序?c#

启动和查看终止应用程序?c#,c#,process,C#,Process,你能帮我吗 我有一个应用程序(app1),可以启动另一个应用程序(app2),而app2正在运行,app1可以对在app2生命周期中更改的文件夹/文件执行一些监视。我能做得很好,跑得很好 然而,我想知道是否有任何方法,我可以监视app2,看看它何时被终止,以便我可以告诉app1 one停止跟踪 我没有app2的源代码,因此无法编辑它以向app1发送信号 谢谢你抽出时间 马特你可以这样做: var p = Process.Start("app2.exe"); // do start watchin

你能帮我吗

我有一个应用程序(app1),可以启动另一个应用程序(app2),而app2正在运行,app1可以对在app2生命周期中更改的文件夹/文件执行一些监视。我能做得很好,跑得很好

然而,我想知道是否有任何方法,我可以监视app2,看看它何时被终止,以便我可以告诉app1 one停止跟踪

我没有app2的源代码,因此无法编辑它以向app1发送信号

谢谢你抽出时间


马特

你可以这样做:

var p = Process.Start("app2.exe");
// do start watching here
p.WaitForExit();
// do stop watching here
请注意,这不是生产质量代码,因为如果app2挂起,此代码将永远等待它。有一个重载允许指定超时,这样您就可以提前完成等待


或者使用
流程。按照Navid的建议退出
事件。

如果使用
流程
使用

请考虑仅当属性为true时才会发生此事件


编辑:如果您需要
app2的输出
@Ivan的答案更好,您需要等待退出。

这是我在一些没有任何问题的项目上使用的(到目前为止)


这看起来确实是一个很好的解决方案,但我可以问一下,这比Navid的建议有什么好处?(反之亦然)谢谢你,等待
WaitForExit
的进程让你的线程忙,所以你不能做任何其他事情。如果您观看的内容具有异步性质,比如操作系统在线程池线程上调用通知,那么就可以了。相反,如果您有一些UI,并且无法执行其线程(因为UI线程繁忙时,UI将“不响应”),则事件更合适。这是同步且直接的。当另一个方法运行时,另一个方法frees不会占用您的应用程序。如果您使用的是OutputDataReceived和ErrorDataReceived处理程序,则可以使用此方法获取输出。
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();
            }
        }

*/