Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VB.NET中停止外部可执行文件?_Vb.net_Windows Services_Process - Fatal编程技术网

如何在VB.NET中停止外部可执行文件?

如何在VB.NET中停止外部可执行文件?,vb.net,windows-services,process,Vb.net,Windows Services,Process,我正在用Visual Basic开发一个Windows服务,它将在启动时启动一个*.exe。它工作得很好。现在,如何在停止此windows服务时停止此*.exe?我的代码如下: Public Class MyWinService Dim RetVal Protected Overrides Sub OnStart(ByVal args() As String) EventLog.WriteEntry("MyService Started") RetVal = Shell("Jo

我正在用Visual Basic开发一个Windows服务,它将在启动时启动一个*.exe。它工作得很好。现在,如何在停止此windows服务时停止此*.exe?我的代码如下:

Public Class MyWinService
Dim RetVal
Protected Overrides Sub OnStart(ByVal args() As String)
    EventLog.WriteEntry("MyService Started")

    RetVal = Shell("JobService.exe", 1)

End Sub

Protected Overrides Sub OnStop()
    EventLog.WriteEntry("MyService Stopped")
End Sub

Protected Overrides Sub OnPause()
    EventLog.WriteEntry("MyService Paused")
End Sub

Protected Overrides Sub OnContinue()
    EventLog.WriteEntry("MyService Resumed")
End Sub

Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
    If command = 200 Then
        EventLog.WriteEntry("Custom Command 200 invoked")
    ElseIf command = 210 Then
        EventLog.WriteEntry("Custom Command 210 invoked")
    End If
End Sub

Private Sub Process1_Exited(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
End Class
使用该方法。这假设您拥有来自的进程句柄,或者您通过其他方式获得了它

由于您已经从
Shell
命令中获得了流程ID,因此可以检索流程:

Dim myProcess = Process.GetProcessById(RetVal)
然后杀了它:

myProcess.Kill()

我注意到您使用的是.NET类,因此我更新了问题标题和标记,以澄清它是VB.NET。可执行文件在Windows中不会停止或启动。进程被启动和停止,可执行文件被加载到其中。感谢您的帮助,正如您所看到的,我没有使用进程。在我的代码中启动。你能再具体一点吗?我对VB还很陌生。非常感谢。