Log Off Inactive Windows User with vb.net

Good Morning All! The family returned from Minnesota safe and sound and happy to see their beds. I of course busied myself with cleaning the house prior to the wife’s return. :).

Okay yesterday I was looking at how to log off a user after a certain period of inactivity, which includes a warning for the user that this is going to occur. I suppose my solution is not elegant but it worked I guess. This is a 2.0 Framework or above app. Make it a great day!


Join me on Facebook

Public Class Form1
    ‘
    ‘setup the class to reboot the computer
    Private Declare Function ExitWindowsEx Lib "user32.dll" ( _
       ByVal uFlags As Long, _
       ByVal dwReserved As Long) As Long

    Private Const EWX_FORCE = 4         ‘const for a forced logoff

    Dim intTimerCount1 As Integer = 0   ‘var for timer1 and setting it to 0
    Dim intTimerCount2 As Integer = 0   ‘var for timer2 and setting it to 0
    Dim oktoreboot As String

    ‘set var for first mouse cords
    Dim MousePosX1 As Integer
    Dim MousePosY1 As Integer
    Dim Mousepos1 As Integer = 0

    ‘set var for second mouse cords
    Dim MousePosX2 As Integer
    Dim MousePosY2 As Integer
    Dim Mousepos2 As Integer = 0

    ‘
    ‘Procedures and Functions
    ‘

    ‘warning sound procedure
    Private Sub WarnWave()
        Try
            My.Computer.Audio.Play("C:\WINDOWS\Media\Windows XP Battery Critical.wav")
        Catch ex As IO.FileNotFoundException

        End Try
    End Sub

    ‘logoff procedure
    Private Sub Command1_Click()

        ‘command to force close apps and logoff user
        ExitWindowsEx(EWX_FORCE, 4)

    End Sub

    ‘
    ‘End Procedures

    Private Sub Form14_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Timer1.Enabled = True           ‘this will start the first timer once the program is fist launched

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

        intTimerCount2 = intTimerCount2 + 1 ‘setup count for timer2
        Label2.Visible = True               ‘turn on label2 to show the countdown timer
        Label2.Text = intTimerCount2

        ‘
        ‘Set the time in seconds for the countdown timer before computer is loffed off
        If intTimerCount2 = 60 Then         ‘sets a 60 second countdown timer before the computer reboots
            ‘

            Timer2.Enabled = False          ‘shuts off timer2 since the computer will now log off
            Call Command1_Click()           ‘calls the logoff command to log user off

        End If

        Select Case Label2.Text             ‘sets up a beep sound every 5 seconds until the computer is logged off or the user presses cancel
            Case "5"
                Call WarnWave()
            Case "10"
                Call WarnWave()
            Case "15"
                Call WarnWave()
            Case "20"
                Call WarnWave()
            Case "25"
                Call WarnWave()
            Case "30"
                Call WarnWave()
            Case "35"
                Call WarnWave()
            Case "40"
                Call WarnWave()
            Case "45"
                Call WarnWave()
            Case "50"
                Call WarnWave()
            Case "55"
                Call WarnWave()

        End Select

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer2.Enabled = False              ‘shuts timer2 off since the user canceled the logoff
        Timer1.Enabled = True               ‘restarts timer1
        intTimerCount2 = 0                  ‘reset the countdown timer since user has pressed cancel
        Label2.Text = 0                     ‘set counter label back to 0
        Me.WindowState = FormWindowState.Minimized  ‘minimize the program
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ‘get first mouse cords
        MousePosX1 = (Control.MousePosition.X – Me.Left)
        MousePosY1 = (Control.MousePosition.Y – Me.Top)
        Mousepos1 = (MousePosX1 + MousePosY1)

        ‘
        ‘Set You inactivty time here in milaseconds
        ‘currently set to 20 min or 1200000 milli seconds
        ‘This means the workstation could log off in 20 min with no mouse activity or
        ‘it might be up to 40 min depending on when the last check was done.

        System.Threading.Thread.Sleep(1200000)

        ‘

    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick

        ‘get second mouse cords
        MousePosX2 = (Control.MousePosition.X – Me.Left)
        MousePosY2 = (Control.MousePosition.Y – Me.Top)
        Mousepos2 = (MousePosX2 + MousePosY2)

        ‘compare the two cords to see if the mouse has moved
        If Mousepos1 = Mousepos2 Then   ‘if = then mouse has not moved durring sleep time
            intTimerCount2 = 0

            Timer2.Enabled = True                       ‘start timer2 (shutdown timer)
            Timer1.Enabled = False                      ‘stops timer1 since timer 2 has been started
            Me.WindowState = FormWindowState.Normal     ‘brings program form to the desktop
            Timer3.Enabled = False
        End If

        Timer1.Enabled = True
        Timer3.Enabled = False

    End Sub
End Class

 

Advertisements
  1. #1 by Maggie on January 7, 2009 - 10:12 am

    Hi my name is maggie and I was wondering if you can send me this project. I tried your code but it does not seem to workAny help would be appreciated.

  2. #2 by Kelly on January 8, 2009 - 9:07 am

    Maggie I attempted to send a message to you but your preferences would not allow it. I can\’t send you the specific project but if you could give me a little more detail on what is going on perhaps I can help you.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: