2012-08-29 260 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实例。她不会很感激失去她的工作。