2013-08-16 51 views
0

我需要用户关闭该.exe文件运行我这样后插入的代码部分(这是FoxPro的EXE):检测.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  
} 

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

+0

您是否尝试过'p.Exited + =(sender,e)=> {/ *您的代码在这里* /};'? –

+0

对不起,但我不知道如何实现此代码。 – Marek

回答

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

尝试

b.Exited += b_Closed; 

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

设置EnableRaisingEventsTrue并听取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) => 
{ 

}; 
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."); 
}