2012-08-29 75 views
-1

要檢查一個進程掛起Word,通過VB.Net 2003項目,我創建一個線程,使進程控制(有問題)。如果在一定數量的毫秒後該進程仍然處於活動狀態,那麼我會從線程中終止該進程。該系統告訴我: 「System.ComponentModel.Win32Exception」, '拒絕訪問'殺死進程winword

的代碼如下:

這是代碼VB:

Imports System 
Imports System.Threading 
Imports System.Threading.Thread 
Imports System.Security.Permissions 
Public Class ThreadMain 
Public strdotName AsString 
Public strdocName AsString 
Public sXML AsString 
Public idProcesso AsInteger 
Public terminate AsInteger 
Public docName AsString 
Public kk AsInteger 
Public finito AsBoolean= False 
Public Function LaunchThread() AsInteger 
Dim myT2 As Threading.Thread 
myT2 = New Threading.Thread(AddressOfMe.DoWork) 
myT2.IsBackground = True 
myT2.Start() 
Try 
Console.WriteLine("Finito: {0}", finito) 
Catch Ex As Exception 
End Try 
Return terminate 
End Function 
*-------** 
Private Sub DoWork() 
Dim j AsInteger 
Dim errore AsString 
Dim trovato AsBoolean 
Dim colProcess AsProcess 
Dim nomeProcess AsProcess() 
Dim chiusura AsBoolean 
Try 
For j = 100 To 20000 
colProcess = Process.GetProcessById(idProcesso) 
If Not colProcess IsNothingThen 
j = j + 1000 
System.Threading.Thread.Sleep(1) 
trovato = True 
Else 
trovato = False 
End If 
Next 
If trovato Then 
chiusura = colProcess.CloseMainWindow() 
colProcess.Close() 
Process.GetProcessById(idProcesso).Kill() 
terminate = 1 
End If 
Catch ex As Exception 
errore = ex.ToString 
terminate = 2 
End Try 
End Sub 'DoWork 
+0

您是否擁有管理員權限?你試圖在遠程機器上殺死的進程是什麼? – Tudor

回答

1

的 「idProcesso」 變量」不是個t代碼中的任何地方。這可能意味着它的價值仍然是零。這意味着你正試圖殺死「系統」進程。這是封裝Windows內核組件的虛假進程。像這樣殺死Windows是不可能的,無論您的用戶權限如何,您都會收到AccessDenied異常。

您需要使用Process.GetProcessesByName()來定位特定進程。

如果這是爲了在使用Microsoft.Office.Interop.Word命名空間時讓Word退出的解決方法,那麼這只是一種用未處理的異常終止自己的程序的好方法。或者就此而言,用戶正在使用的另一個Word實例。她不會很感激失去她的工作。