Search code examples
vb.netreporting-servicesprogress-barservicecontroller

How to enable Live Service Controller Reporting?


Application Screenshot:

enter image description here

Application Code:

Dim myController As New System.ServiceProcess.ServiceController("SQL Server (SQLEXPRESS)")
Sub ed()
    If txtNum.Text = "Started" Then
        btnStart.Enabled = False
        btnStop.Enabled = True
    ElseIf txtNum.Text = "Stopped" Then
        btnStop.Enabled = False
        btnStart.Enabled = True
    End If
End Sub
Sub Na1()
    If myController.Status = ServiceProcess.ServiceControllerStatus.Running Then
        txtNum.Text = "Started"
    ElseIf myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
        txtNum.Text = "Stopped"
    End If
End Sub
Private Sub Test_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Na1()
    ed()
End Sub
Private Sub Test_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    Me.Refresh() '////////This one is not required
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    ProgressBar1.Value = 0
    Try
        myController.Start()
    Catch ex As Exception
        MsgBox(ex.Message) 
    Finally
        ProgressBar1.Value = 100
        Na1()'//////////////////////////////////////////////Edit Code here
    End Try
End Sub

Private Sub txtNum_TextChanged(sender As Object, e As EventArgs) Handles txtNum.TextChanged
    ed()
End Sub

Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    ProgressBar1.Value = 0
    Try
        myController.Stop()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        ProgressBar1.Value = 100
        Na1() '//////////////////////////////////////////////Edit Code here
    End Try
End Sub

You need to stop SQL Service to Copy .mdf File and it is very irritating by Stopping/Starting the service manually from 'Services' so I tried to make it easier by using Vb.Net coding...


Solution

  • Edit the Following codes,

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    ProgressBar1.Value = 0
    Try
        myController.Start()
    Catch ex As Exception
        MsgBox(ex.Message) 
    Finally
        ProgressBar1.Value = 100
        Na1()'//////////////////////////////////////////////Edit Code here
    End Try
    End Sub
    

    To

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        ProgressBar1.Value = 0
        Try
            myController.Start()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            ProgressBar1.Value = 100
            myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)'//Add
            Na1()
        End Try
    End Sub
    

    And

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
    ProgressBar1.Value = 0
    Try
        myController.Stop()
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        ProgressBar1.Value = 100
        Na1() '//////////////////////////////////////////////Edit Code here
    End Try
    End Sub
    

    To

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        ProgressBar1.Value = 0
        Try
            myController.Stop()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            ProgressBar1.Value = 100
            myController.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped) '//Add
            Na1()
        End Try
    End Sub
    

    And all is done!