Skip to content

Windows Service – Restart windows service after idle timeout

April 27, 2012

UltraVNC’s idle timeout setting does not work so I created this service to monitor idle time on the computer and then restart the service to disconnect all current connections.
The default timeout is set to 120s. This can be changed through the registry setting.

Imports System.Runtime.InteropServices
Imports System.ServiceProcess

Public Class VNCRestart
    Private tMonitor As New System.Timers.Timer(5000)
    Private idle As Boolean

    Protected Overrides Sub OnStart(ByVal args() As String)
        AddHandler tMonitor.Elapsed, AddressOf tMonitor_Tick
        tMonitor.Enabled = True
    End Sub

    Protected Overrides Sub OnStop()
        tMonitor.Enabled = False
        RemoveHandler tMonitor.Elapsed, AddressOf tMonitor_Tick
    End Sub

    Private Sub tMonitor_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Dim tout = Timeout * 1000
        If GetIdleTime() > tout And Not idle Then
            RestartService("uvnc_service", 30000)
            idle = True
        ElseIf GetIdleTime() < 60000 And idle Then
            idle = False
        End If
    End Sub

    Shared ReadOnly Property Timeout() As Integer
            Timeout = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\VNC Idle Timeout\", "Timeout", 0)
            If Timeout.Equals(0) Then
                Timeout = 120
                My.Computer.Registry.SetValue("HKEY_LOCAL_MACHINE\Software\VNC Idle Timeout\", "Timeout", Timeout)
            End If
        End Get
    End Property

    'Create a structure to pass the reference to the dll.
    Public Structure LASTINPUTINFO
        Public cbSize As UInteger
        Public dwTime As UInteger
    End Structure

    'Importing the Dll & declaring the neccessary funtion
    <DllImport("user32.dll")> _
    Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function

    'Function to return the idle time
    Private Function GetIdleTime() As Integer
        'Creating the object of the structure
        Dim lastOne As New LASTINPUTINFO()

        lastOne.cbSize = CUInt(Marshal.SizeOf(lastOne))
        lastOne.dwTime = 0

        Dim idleTime As Integer = 0

        'To get the total time after starting the system.
        Dim tickCount As Integer = Environment.TickCount

        'Calling the dll funtion and getting the last input time.
        If GetLastInputInfo(lastOne) Then
            idleTime = tickCount - CInt(lastOne.dwTime)
            Return idleTime
            Return 0
        End If
    End Function

    Public Sub RestartService(serviceName As String, timeoutMilliseconds As Integer)
        Dim service As New ServiceController(serviceName)
            Dim millisec1 = Environment.TickCount
            Dim timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds)

            service.WaitForStatus(ServiceControllerStatus.Stopped, timeout)

            ' count the rest of the timeout
            Dim millisec2 = Environment.TickCount
            timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - millisec2 - millisec1)

            service.WaitForStatus(ServiceControllerStatus.Running, timeout)
        End Try
    End Sub
End Class



From → .Net, CodeProject

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: