views:

27

answers:

1

I am developping a Windows Service in Visual Basic, witch will start an *.exe when starting. It's working pretty fine. Now how can I stop this *.exe while stopping this windows service? My code is as below:

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
+2  A: 

Use the Process.Kill method. This assumes that you have the process handle from Process.Start, or that you have obtained it in some other way.

As you already have the process ID from the Shell command, you can retrieve the process:

Dim myProcess = Process.GetProcessById(RetVal)

Then Kill it:

myProcess.Kill()
Oded
The Shell command returns the process id
Luhmann
Thanks for helping, as you can see, I am not using Process.Start in my code. Can you be a little bit more specified? I am pretty new to VB. Thank you.
Ray