当一个不同的应用程序关闭时强制关闭一个应用程序,然后关闭它自己

时间:2013-08-24 00:41:15

标签: c#

所以,我正在写一个程序,这是它的开始!

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.Process.Start(@"C:\\Windows\\ehome\\ehshell.exe");
            System.Diagnostics.Process.Start(@"E:\\Xpadder\\WMC.xpaddercontroller");
        }
    }
}

它只是打开两个文件。我想要它做的也是等到它检测到ehshell.exe何时停止运行然后强制另一个程序(在这种情况下是xpadder)也结束。

我已经看过这样做的代码,但我不是最好的C#而不是100%肯定我正在寻找的东西!

2 个答案:

答案 0 :(得分:1)

        var p1 = new ProcessStartInfo
        {
            FileName = atomicParsleyFile,
            Arguments = atomicParsleyCommands,
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true
        };

        var proc = new Process { StartInfo = p1 };

        if (!proc.Start())
        {
            throw new ApplicationException("Starting atomic parsley failed!");
        }

                    /*Repeat above for second process here */

        Console.WriteLine(proc.StandardOutput.ReadToEnd());

        proc.WaitForExit(); //Run AtomicParsley and Wait for Exit
                    proc2.Kill();

答案 1 :(得分:0)

    static void Main(string[] args)
    {
        System.Diagnostics.Process p1 = System.Diagnostics.Process.Start(@"C:\\Windows\\ehome\\ehshell.exe");
        System.Diagnostics.Process p2 = System.Diagnostics.Process.Start(@"E:\\Xpadder\\WMC.xpaddercontroller");
        p1.Exited += (s,e) => {
           if(!p2.HasExited) p2.Kill();
        };

    }