Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27212

help needed with function

$
0
0
hello all!

I have a specific problem and I do not understand it, thus i can't get arround it.

the context:
trying to build a small utility that will remotely restart 2 services: "cpsvc" and "spooler".
to this end, i've made a form with an input textbox, a start button, and an output textbox.

generic variables:
Code:

    Dim serverName As String 'variable to store the input from the user - should be target server's name
    Dim pingTest As Integer
    Dim svcStatus As String
    Dim svcCtrl As ServiceController

start button code:
Code:

    Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
        serverName = Input.Text 'here the "serverName" variable is assigned the content of the input textbox, which should contain a server's hostname
        If CheckServer() = 1 Then 'the CheckServer() function verifies that the target server exists and it can be contacted - it is irelevant here/now
            Call proceedAction()
        End If
    End Sub

content of the proceedAction() function:
Code:

    Public Function proceedAction()
        Dim runtime As Integer
        runtime = StopService("cpsvc")

        If runtime = 1 Then
            Output.Text += vbCrLf & "Done1"
            runtime = StopService("Spooler")
        End If
        If runtime = 1 Then
            Output.Text += vbCrLf & "Done2"
            runtime = StartService("Spooler")
        End If
        If runtime = 1 Then
            Output.Text += vbCrLf & "Done3"
            runtime = StartService("cpsvc")
        End If
        If runtime = 1 Then
            Output.Text += vbCrLf & "Done4"
        End If
    End Function




At runtime, the following function loops indefinetely:
Code:

    Public Function StopService(ByVal svcName As String)
        Dim cycleCheck As Integer
        Dim funcResultStop As Integer

        svcCtrl = New ServiceController
        svcCtrl.MachineName = serverName
        svcCtrl.ServiceName = svcName

        cycleCheck = 0
        funcResultStop = 0

Retry:
        If cycleCheck < 2 Then
lastRetry:
            svcStatus = svcCtrl.Status.ToString
            Output.Text += vbCrLf & svcName & " " & svcStatus & cycleCheck
            Select Case svcStatus
                Case "Stopped"
                    Output.Text += vbCrLf & svcName & " already Stopped"
                    funcResultStop = 1
                Case "Running"
                    Try
                        svcCtrl.Stop()
                        Output.Text += vbCrLf & svcName & "Stopping"
                        cycleCheck = cycleCheck + 1
                        svcStatus = "Stopped"
                    Catch ex As Exception
                        ' Throw ex
                        svcStatus = ""
                    End Try
                    Threading.Thread.Sleep(5000)
                    GoTo Retry
                Case "Start_pending"
                    Threading.Thread.Sleep(3000)
                    cycleCheck = cycleCheck + 1
                    GoTo Retry
                Case "Stop_pending"
                    Threading.Thread.Sleep(3000)
                    cycleCheck = cycleCheck + 1
                    GoTo Retry
                Case "Paused"
                    svcCtrl.Stop()
                    GoTo Retry
                Case Else
                    Try
                        svcCtrl.Stop()
                        GoTo Retry
                    Catch ex As Exception
                        GoTo lastResort
                    End Try
            End Select
        Else
lastResort:
            ' as "last resort" the program attempts to kill the process for the service
            If cycleCheck < 3 Then
                Select Case svcName
                    Case "cpsvc"
                        ' kill process
                        Try
                            Shell("taskkill /s " & serverName & " /f CpSvc.exe")
                        Catch ex As Exception
                            Throw ex
                        End Try
                    Case "Spooler"
                        ' kill process
                        Try
                            Shell("taskkill /s " & serverName & " /f spoolsv.exe")
                        Catch ex As Exception
                            Throw ex
                        End Try
                End Select
                Threading.Thread.Sleep(2000) 'wait 2 seconds to give the kill command the chance to execute
                cycleCheck = cycleCheck + 1
                GoTo lastRetry
            Else
                ' put out error message for "service <svcName> can't be manipulated"
                Output.Text += vbCrLf & svcName & "can't be manipulated, please create a ticket to Citrix/Wintel team"
            End If
        End If
        Return funcResultStop
    End Function

when it's being first parsed, it works as intended, but then it's just stuck in the exception part of stopping the service. for some reason, the service status does not get updated...
this is the output:
Attachment 98875
Attached Images
 

Viewing all articles
Browse latest Browse all 27212


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>