2023年2月14日 星期二

RestartService

 Public Shared Function RestartService(ByVal ServiceName As String) As Boolean

    Dim mySC As ServiceProcess.ServiceController

    Try

        mySC = New ServiceProcess.ServiceController(ServiceName)

        mySC.Stop()

        mySC.Start()

        Return mySC.Status = ServiceProcess.ServiceControllerStatus.Running

    Catch

        Return False

    End Try

End Function



' restart without bothering about success or failure

RestartService("MSSQLSERVER")


'or like this if you are too concerned ;)

If RestartService("MSSQLSERVER") Then

    MsgBox("Service restarted")

Else

    MsgBox("Unable to restart the service.")

End If


----------------------------------------

@ECHO OFF


net stop ServiceName

net start ServiceName


2023年2月13日 星期一

delay 的方法

  Sub delay(ByVal delay_ms As Integer)

        Dim tspan As New TimeSpan

        Dim tstart = Now

        While tspan.TotalMilliseconds < delay_ms

            tspan = Now - tstart

            Application.DoEvents()

        End While

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Label1.Text = "Hello"

        delay(100) 'leaks 572 ns every 10 ms

        Label1.Text = "Salut"

    End Sub