为了检查进程是否挂起 Word,通过 VB.Net 2003 项目,我创建了一个线程来进行进程控制(有问题)。如果在一定数量的毫秒后该进程仍然处于活动状态,我会从线程中终止该进程。系统告诉我“System.ComponentModel.Win32Exception”,“拒绝访问”
代码如下:
这是代码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