2010-02-18 41 views
0

想象一下,我們有兩個.net應用程序。應用程序「A」使用System.Diagnostics.Process類啓動應用程序「B」。然後「A」想通過方法Process.Kill殺死「B」。 「B」如何確定有人在殺害他?如何確定查殺應用程序

+1

重複http://stackoverflow.com/questions/1372250/how-to-detect-a-kill-process-event – 2010-02-18 14:19:46

回答

0

我認爲應用程序無法響應被殺......我認爲它在操作系統級別運行更像使用任務管理器。

在這種情況下使用Process.Kill()可能不正確,您能否提供更多關於您嘗試解決的問題的信息?

+0

是的,我發現「一個進程無法阻止自己被終止。」我認爲我們沒有機會確定它。 http://msdn.microsoft.com/en-us/library/ms686714(VS.85).aspx – alga 2010-02-19 06:17:15

0

也許你可以進程B的代碼中這樣嘗試...

 
// We're in Process B.... 
System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess(); 
proc.EnableRaisingEvents = true; 
proc.Exited += new EventHandler(proc_Exited); 

static void proc_Exited(object sender, EventArgs e) 
{ 
    // Handle here that we got killed... 
} 

我不能肯定地說,這將工作...發送一個「殺」到OS的本質一個進程是依賴於實現的,因此,進程B沒有保證萬無一失的方式知道它正在被殺死。正如你還沒有明確說明過程B是一個託管/非託管過程,我將假設它確實是管理的基礎,因爲標籤是'.net',如果它是WinForm應用程序,可能是winForms中的一個Closing事件將在該事件處理的論點的理由或使用ApplicationDomain的情況下,如下圖所示:

 
AppDomain thisDom = AppDomain.CurrentDomain; 
thisDom.ProcessExit += new EventHandler(thisDom_ProcessExit); 
// 

static void thisDom_ProcessExit(object sender, EventArgs e) 
{ 
    // Handle the situation here where the AppDomain is going to be unloaded and killed! 
} 

希望這有助於 最好的問候, 湯姆。