none
Events handling in VB.Net RRS feed

  • Question

  • I have created an form in vb.net,which is as below.

    When this form is loaded, it will show the duration, by starting the timer. This will keep on counts the time and when it reaches 9 hr, it will show some msgbox saying 9hr got completed.

    I want to make the timer to get paused when I lock ((Windows+L)) my system and the timer should resume again when I unlock my system.

    If anyone knows how I can handle this event, please let me know.

    Thanks in advance

    Monday, October 8, 2012 1:18 PM

Answers

  • See if the SystemEvents Class (http://msdn.microsoft.com/library/microsoft.win32.systemevents.aspx) helps you.

    Paulo Morgado

    Monday, October 8, 2012 2:10 PM
  • Please find an example which will guide you. It has a counter which will stop while you lock the system and resume after you unlock the system.

    Imports Microsoft.Win32
    Imports System.Timers
    Public Class Form1
        Private Shared aTimer As System.Timers.Timer
        Private Shared Count As Integer
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            AddHandler SystemEvents.SessionSwitch, AddressOf checkLockUnlock
            ' Create a timer with a  second interval.
            aTimer = New System.Timers.Timer(1000)
            ' Hook up the Elapsed event for the timer. 
            AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
            ' Set the Interval to 2 seconds (2000 milliseconds).
            aTimer.Interval = 2000
            aTimer.Enabled = True
            Count = 0
        End Sub
        ' Specify what you want to happen when the Elapsed event is  
        ' raised. 
        Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
            Count = Count + 1
            MessageBox.Show("The count is " & Count)
        End Sub
        Public Sub checkLockUnlock(ByVal sender As Object, ByVal e As SessionSwitchEventArgs)
            If e.Reason = SessionSwitchReason.SessionLock Then
                aTimer.Enabled = False
            Else
                aTimer.Enabled = True
            End If
        End Sub
    End Class


    If a post answers your question, please click "Mark As Answer" and "Mark as Helpful" on that post.

    Monday, October 8, 2012 2:14 PM

All replies

  • See if the SystemEvents Class (http://msdn.microsoft.com/library/microsoft.win32.systemevents.aspx) helps you.

    Paulo Morgado

    Monday, October 8, 2012 2:10 PM
  • Please find an example which will guide you. It has a counter which will stop while you lock the system and resume after you unlock the system.

    Imports Microsoft.Win32
    Imports System.Timers
    Public Class Form1
        Private Shared aTimer As System.Timers.Timer
        Private Shared Count As Integer
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            AddHandler SystemEvents.SessionSwitch, AddressOf checkLockUnlock
            ' Create a timer with a  second interval.
            aTimer = New System.Timers.Timer(1000)
            ' Hook up the Elapsed event for the timer. 
            AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
            ' Set the Interval to 2 seconds (2000 milliseconds).
            aTimer.Interval = 2000
            aTimer.Enabled = True
            Count = 0
        End Sub
        ' Specify what you want to happen when the Elapsed event is  
        ' raised. 
        Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
            Count = Count + 1
            MessageBox.Show("The count is " & Count)
        End Sub
        Public Sub checkLockUnlock(ByVal sender As Object, ByVal e As SessionSwitchEventArgs)
            If e.Reason = SessionSwitchReason.SessionLock Then
                aTimer.Enabled = False
            Else
                aTimer.Enabled = True
            End If
        End Sub
    End Class


    If a post answers your question, please click "Mark As Answer" and "Mark as Helpful" on that post.

    Monday, October 8, 2012 2:14 PM