Hey guys I'm trying to stop one service and then restart a different service.
Right now I have this code to restart a service
Public Sub RestartService(ByVal myServiceName As String)
Dim DataSource As String = TextBox1.Text Dim sStatus As String Dim myController As ServiceController myController = New ServiceController myController.MachineName = DataSource myController.ServiceName = myServiceName TextBox2.Text += "Stopping service """ & myServiceName & """...." & vbNewLine If myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then TextBox2.Text += "Service """ & myServiceName & """ is already stopped" & vbNewLine Else Try myController.Refresh() sStatus = myController.Status.ToString myController.Stop() myController.WaitForStatus(ServiceControllerStatus.Stopped) TextBox2.Text += "Service """ & myServiceName & """ stopped..." & vbNewLine TextBox2.Text += "Starting service """ & myServiceName & """...." & vbNewLine myController.Refresh() sStatus = myController.Status.ToString myController.Start() myController.WaitForStatus(ServiceControllerStatus.Running) TextBox2.Text += "Service """ & myServiceName & """ started..." & vbNewLine Catch exp As Exception TextBox2.Text += exp.Message End Try End If End Sub
And this one to Stop a service:
Public Sub StopService(ByVal myServiceName As String)
Dim DataSource As String = TextBox1.Text Dim sStatus As String Dim myController As ServiceController myController = New ServiceController myController.MachineName = DataSource myController.ServiceName = myServiceName TextBox2.Text += "Stopping service """ & myServiceName & """...." & vbNewLine If myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then TextBox2.Text += "Service """ & myServiceName & """ is already stopped" & vbNewLine Else Try myController.Refresh() sStatus = myController.Status.ToString myController.Stop() myController.WaitForStatus(ServiceControllerStatus.Stopped) TextBox2.Text += "Service """ & myServiceName & """ stopped..." & vbNewLine Catch exp As Exception TextBox2.Text += "Could not stop service """ & myServiceName & """" & vbNewLine End Try End If End Sub
But when I try to click this button, it only stops the first service and it doesn't restart the second service.
Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
If MsgBox("Are you sure you want to restart Spooler Service?", vbYesNo) = vbNo Then Exit Sub StopService("LPDSVC") RestartService("Spooler") End Sub
What am I missing inbetween stop and restartservice?