繁体   English   中英

启动并观看终止申请? C#

[英]Launching and watching a application for termination? c#

想知道你是否可以帮助我?

我有一个应用程序(app1),可以启动另一个应用程序(app2),而app2运行app1能够对在app2的生命周期中更改的文件夹/文件做一些保护。 我可以做得很好并且运行完美。

但是我想知道是否有任何方法可以监控app2以查看它何时被终止以便我可以告诉app1停止跟踪?

我没有app2的源代码,因此我无法对其进行编辑以向app1发送信号。

谢谢你的时间。

马特

你可以这样做:

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

请注意,它不是生产质量的代码,因为如果app2将挂起 - 此代码将永远等待它。 有超载允许指定超时,因此您可以事先完成等待。

或者使用Process.Exited事件作为Navid建议。

如果您正在使用Process使用Process.Exited事件

仅当EnableRaisingEvents属性为true时,才会发生此事件

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

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

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

*/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM