Friday, June 24, 2016

Restart Windows Services VB.NET

Restart Windows Services VB.NET

Imports System
Imports System.ServiceProcess
Imports System.IO
Imports System.Threading
'------------------------------------------------------------------------------------

Public Class Form1
    Dim x As Integer

    '------------------------------------------------------------------------------------


    Public Shared Function RestartService(ByVal ServiceName As String) As Boolean
        Dim mySC As ServiceProcess.ServiceController
        mySC = New ServiceProcess.ServiceController(ServiceName)

        If mySC.Status.ToString = "Running" Then
            Form1.Label1.Text = "STOPPING SERVICES"
            mySC.Stop()
            Form1.x = 5
            Form1.Timer1.Enabled = True
        End If

        If mySC.Status.ToString = "Stopped" Then
            Form1.Label1.Text = "RUNNING SERVICES"
            mySC.Start()
            Form1.x = 5
            Form1.Timer2.Enabled = True
        End If

    End Function

    '------------------------------------------------------------------------------------

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = ""
        Label2.Text = ""
        RestartService(TextBox1.Text)
    End Sub

    '------------------------------------------------------------------------------------

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim mySC As ServiceProcess.ServiceController
        mySC = New ServiceProcess.ServiceController(TextBox1.Text)

        If x = 0 Then
            If mySC.Status.ToString = "Stopped" Then

                Label1.Text = "RERUNNING SERVICES"
                Label2.Text = ""
                mySC.Start()
                Timer1.Enabled = False
                x = 5
                Timer2.Enabled = True
            Else
                x = 5
                Timer1.Enabled = True
            End If

        Else
            x = x - 1
            Label2.Text = "TRYING. . . ." & " " & x
            TextBox2.Text = (mySC.Status.ToString())

        End If
    End Sub

    '------------------------------------------------------------------------------------

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim mySC As ServiceProcess.ServiceController
        mySC = New ServiceProcess.ServiceController(TextBox1.Text)

        If x = 0 Then
            If mySC.Status.ToString = "Running" Then
                Label1.Text = "DONE"
                Label2.Text = ""
                Timer2.Enabled = False
            Else
                x = 5
                Timer2.Enabled = True
            End If


        Else
            x = x - 1
            Label2.Text = "TRYING. . . ." & " " & x
            TextBox2.Text = (mySC.Status.ToString())

        End If
    End Sub

    '------------------------------------------------------------------------------------


End Class

No comments: