2013-08-16 48 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."); 
}