0

我需要在用户关闭我这样运行的 .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    
}

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

4

5 回答 5

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.");
}
于 2013-08-16T10:12:56.110 回答
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...
}
于 2013-08-16T10:06:54.773 回答
1

尝试

b.Exited += b_Closed;

void b_Closed(object sender, EventArgs e)
{
    // mycode    
}
于 2013-08-16T10:07:03.150 回答
1

设置并收听EnableRaisingEvents事件。TrueExited

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) => 
{

};
于 2013-08-16T10:07:06.117 回答
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");
    }
于 2013-08-16T10:07:07.803 回答