2011-01-07 59 views
0

我的應用程序創建嵌入Windows窗體中的一個新的winword實例。殺死Winword的特定實例

new Microsoft.Office.Interop.Word.ApplicationClass() 

一旦用戶完成編輯,我關閉窗口,但雙字實例仍在後臺運行。

我該如何殺死那個特定的實例?我知道如何殺死機器上運行的所有實例,但這不是一個選項。

+1

你最終使用的情況下也有一個用戶的文檔在另一個窗口中打開? – Gabe 2011-01-07 17:58:58

+0

它是一個只有少數用戶的內部應用程序,所以他們不會從我爲他們創建的實例中創建新文檔。但是,如果他們有單獨打開我的Windows窗體以外的窗口我不能關閉它 – user48408 2011-01-07 18:09:08

+0

可能重複的問題[懸掛互操作COM對象](http://stackoverflow.com/questions/4503309/problem-with-hanging-interop -com-objects) – 2011-01-07 18:59:47

回答

1

你需要調用

application.Quit() 

在你的代碼。

我的系統(這是一個excel處理工具)必須做的一件事是如果在Quit的事情還在繼續之後進行殺死。

var openedExcel = System.Diagnostics.Process.GetProcessesByName("EXCEL"); 
      if (openedExcel.Any()) 
      { 
       foreach (var excel in openedExcel) 
       { 
        try { excel.Kill(); } 
        catch { } 
       } 
      } 
0

我建議你把/添加到Flaviotsf的回答下面的代碼 - 這將決定是在運行什麼實際過程:

如果
Process[] myProcList = Process.GetProcesses(); 
for (int i = 0; i <= myProcList.Length - 1; i ++) { 
    string strProcessName = myProcList [i].ProcessName; 

    string strProcessTitle = myProcList [i].MainWindowTitle(); 
     //check for your process name. 
    if (strProcessName.ToLower().Trim().Contains("access")) { 

     myProcList [i].Kill(); 
    } 
}