检测.exe是否已关闭

时间:2013-08-16 09:56:52

标签: c# winforms exe

我需要在用户关闭.exe之后插入部分代码,我这样运行(它是foxpro exe):

private void button1_Click(object sender, EventArgs e)
 {
string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
Process.Start(openexe);
}

我认为它可以像这样工作:

string otevriExe = @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
string b = Process.Start(otevriExe);
                 b.Closed += b_Closed;
void b_Closed(object sender, EventArgs e)
{
    // mycode    
}

有人请帮助我改进我的代码以便它可以工作吗?谢谢大家的时间和答案。

5 个答案:

答案 0 :(得分:1)

使用Exited事件(注意:您需要将EnableRaisingEvents设置为true),例如:

private void button1_Click(object sender, EventArgs e)
{
    string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
    var proc = new Process();
    proc.StartInfo = new ProcessStartInfo(openexe);
    proc.EnableRaisingEvents = true;
    proc.Exited += new EventHandler(proc_Exited);
    proc.Start();
}

private void proc_Exited(object sender, EventArgs e)
{
    // the process has exited...
}

这是异步方式,对于同步方法,您可以使用WaitForExit方法:

private void button1_Click(object sender, EventArgs e)
{
    string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
    var proc = new Process();
    proc.StartInfo = new ProcessStartInfo(openexe);
    proc.Start();
    proc.WaitForExit();
    // here the process has exited...
}

答案 1 :(得分:1)

尝试

b.Exited += b_Closed;

void b_Closed(object sender, EventArgs e)
{
    // mycode    
}

答案 2 :(得分:1)

EnableRaisingEvents设为True并收听Exited事件。

string otevriExe = @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
Process b = Process.Start(otevriExe);
b.EnableRaisingEvents = true;
b.Exited += (s, e) => 
{

};

答案 3 :(得分:1)

你可以尝试:

string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
Process p = new Process();
p.StartInfo.FileName = openexe;
p.Start();
p.WaitForExit();
//do stuff here

编辑:看到你在点击按钮时启动它,而不是使用事件处理程序:

 private void button1_Click(object sender, EventArgs e)
    {
        Process p = new Process();
        string openexe= @"C:\Users\marek\Documents\Visual Studio 2012\Projects\tours\tours\bin\Debug\netpokl.exe";
        p.StartInfo.FileName = openexe;
        p.EnableRaisingEvents = true;
        p.Exited +=new EventHandler(p_Exited);
        p.Start();            
    }

 private void p_Exited(object sender, EventArgs e)
    {
        //Do stuff here
        MessageBox.Show("Exited");
    }

答案 4 :(得分:1)

你可以做你的事onExited事件。喜欢这个

public static void Main(string[] args)
{
    MyProcess p = new MyProcess();
    p.StartInfo.FileName = "notepad.exe";
    p.EnableRaisingEvents = true;
    p.Exited += new EventHandler(myProcess_HasExited);
    p.Start();       
}
private static void myProcess_HasExited(object sender, System.EventArgs e)
{
    Console.WriteLine("Process has exited.");
}