CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jun 2009
    Posts
    35

    [RESOLVED] Check if PC is in sleep mode

    ello frnds,i want to check if the computer is in sleep mode or not...Suppose my project is in normal state..I change the state to sleep mode,I want that now when the computer wakes from sleep mode,I want that my project is in minimized state..How to check dat?

    Code:
    Imports Microsoft.Win32
    
    Public Class Form1
        Public Sub New()
            MyBase.New()
    
    
            InitializeComponent()
            AddHandler SystemEvents.PowerModeChanged, _
            AddressOf SystemEvents_PowerModeChanged
    
        End Sub
    
        Private Sub SystemEvents_PowerModeChanged( _
        ByVal sender As Object, _
        ByVal e As PowerModeChangedEventArgs)
            MsgBox(e.Mode.ToString())
        End Sub
    
    End Class
    BUt event SystemEvents_PowerModeChange is not fire when i change the mode..can somebody help me?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Check if PC is in sleep mode

    I don't think you can detect when it wakes up. Any code that you write will be in the sleep state anyways
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2009
    Posts
    35

    Re: Check if PC is in sleep mode

    ok it it possible to detect it..when PC goes to sleep mode.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Check if PC is in sleep mode

    Yes. Just not the opposite
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Check if PC is in sleep mode

    In order to wake up the computer you'll need to use the CreateWaitableTimer and SetWaitableTimer APIs, to specify a time to wait for the wake up call. Then, you could use the WaitForSingleObject API to check if the Wake Up system event has happened.

    HEre's a small sample :
    Code:
    Imports System
    Imports System.Windows.Forms
    Imports System.Runtime.InteropServices
    Imports System.Threading
    
    Public Class Form1
        Public Function CreateWaitableTimer(ByVal lpTimerAttributes As IntPtr, ByVal bManualReset As Boolean, _
        ByVal lpTimerName As String) As IntPtr
        End Function
        Public Function SetWaitableTimer(ByVal hTimer As IntPtr, _
         <[In]()> ByRef pDueTime As Long, ByVal lPeriod As Integer, _
         ByVal pfnCompletionRoutine As IntPtr, ByVal lpArgToCompletionRoutine As IntPtr, _
         ByVal fResume As Boolean) As Boolean
        End Function
    
        Public Declare Auto Function WaitForSingleObject Lib "kernel32" (ByVal handle As IntPtr, _
        ByVal milliseconds As UInteger) As Int32
    
        Private Sub WaitForWakeUp()
            Dim WaitTime As Long = -2000000
            Dim TimeHandle As IntPtr
            TimeHandle = CreateWaitableTimer(IntPtr.Zero, True, SetWaitableTimer(TimeHandle, WaitTime, 0, _
            IntPtr.Zero, IntPtr.Zero, True))
            TimeHandle = CreateWaitableTimer(IntPtr.Zero, True, SetWaitableTimer(TimeHandle, WaitTime, 0, _
            IntPtr.Zero, IntPtr.Zero, True))
            Dim Duration As UInteger = 4294967295
            Dim RetVal As Integer = WaitForSingleObject(TimeHandle, Duration)
            MessageBox.Show("Aah, Now I'm Woken Up From My Sleep!")
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WaitForWakeUp()
        End Sub
    End Class
    Try it, and see if it works

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Check if PC is in sleep mode

    He wants to know WHEN it wakes up.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Check if PC is in sleep mode

    Quote Originally Posted by HanneSThEGreaT View Post
    In order to wake up the computer you'll need to use the CreateWaitableTimer and SetWaitableTimer APIs, to specify a time to wait for the wake up call. Then, you could use the WaitForSingleObject API to check if the Wake Up system event has happened.

    Try it, and see if it works
    The Task Scheduler would do that with one line! (Especially in Vista+)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Check if PC is in sleep mode

    Quote Originally Posted by dglienna View Post
    He wants to know WHEN it wakes up.
    David, you said it may not be possible. I provided a decent way to do it, and how it should be implemented. So, what is the / your problem &#191;

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Check if PC is in sleep mode

    This is not about who helps who. This is not about whose code is more right, or whatever. There is no "My way is better than yours" etc. This is about helping people and learning from it.

  10. #10
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: Check if PC is in sleep mode

    If the time to wake up is unspecified, and it relies on user input, ie
    (mouse/keyboard/powerbutton depends on the S0, S1, S3 bios mode settings), then GetLastInputInfo may be helpful too, but...


    But then I looked up the message WM_POWER, which does seem to have the ability to know when it's just been resumed,
    if you use PWR_SUSPENDRESUME in wParam.

    http://msdn.microsoft.com/en-us/libr...45(VS.85).aspx


    EDIT:
    WM_POWERBROADCAST PBT_APMRESUMESUSPEND
    http://msdn.microsoft.com/en-us/libr...47(VS.85).aspx

    I don't have a sample, but if you guys agree that it should work, I'll try and make a subclass example.
    Last edited by TT(n); July 28th, 2009 at 06:32 AM.

  11. #11
    Join Date
    Jun 2009
    Posts
    35

    Re: Check if PC is in sleep mode

    Thx both of you..what i was looking for is below-
    No Suppose ur project is running,& when u press sleep message is there,Suspending & when the system wake up message Resuming

    Code:
    Imports Microsoft.Win32
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler SystemEvents.PowerModeChanged, AddressOf mypmc
        End Sub
    
        Public Sub mypmc(ByVal sender As Object, ByVal e As PowerModeChangedEventArgs)
            If e.Mode = PowerModes.Suspend Then
                MsgBox("Suspending")
            End If
            If e.Mode = PowerModes.Resume Then
                MsgBox("Resuming")
            End If
        End Sub
    End Class

  12. #12
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: [RESOLVED] Check if PC is in sleep mode

    Works like a charm, without API!



    This helped me.
    Added to your reputation
    Last edited by TT(n); July 28th, 2009 at 06:46 AM.

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: [RESOLVED] Check if PC is in sleep mode

    Very nice work indeed

    I also learnt something new today!

    Also, added to your reputation

  14. #14
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: [RESOLVED] Check if PC is in sleep mode

    Wow this was exactly what I need for my osk project.


    I took it a little further, so that our apps can have full power detection.
    Using the same syntax, we can detect the user switching, logging off, shutting down etc.:
    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf SysSuspend
            AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf SysLogOff
            AddHandler Microsoft.Win32.SystemEvents.SessionSwitch, AddressOf SysSwitch
            AddHandler Microsoft.Win32.SystemEvents.SessionEnded, AddressOf SysLoggedOff
        End Sub
    
        Public Sub SysLogOff(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionEndingEventArgs)
            If e.Reason = Microsoft.Win32.SessionEndReasons.Logoff Then
                MessageBox.Show("user logging off")
            ElseIf e.Reason = Microsoft.Win32.SessionEndReasons.SystemShutdown Then
                MessageBox.Show("system is shutting down")
            End If
        End Sub
    
        Public Sub SysLoggedOff(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionEndedEventArgs)
            If e.Reason = Microsoft.Win32.SessionEndReasons.Logoff Then
                MessageBox.Show("user logged off")
            ElseIf e.Reason = Microsoft.Win32.SessionEndReasons.SystemShutdown Then
                MessageBox.Show("system has shut down")
            End If
        End Sub
    
        Public Sub SysSwitch(ByVal sender As Object, ByVal e As Microsoft.Win32.SessionSwitchEventArgs)
            If e.Reason = Microsoft.Win32.SessionSwitchReason.ConsoleConnect Then
                MessageBox.Show("Console connect")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.ConsoleDisconnect Then
                MessageBox.Show("Console disconnect")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.RemoteConnect Then
                MessageBox.Show("Remote Connect")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.RemoteDisconnect Then
                MessageBox.Show("Remote Disconnect")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLock Then
                MessageBox.Show("Session Lock")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLogoff Then
                MessageBox.Show("Session Logoff")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionLogon Then
                MessageBox.Show("Session Logon")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionRemoteControl Then
                MessageBox.Show("Session Remote Control")
            ElseIf e.Reason = Microsoft.Win32.SessionSwitchReason.SessionUnlock Then
                MessageBox.Show("Session Unlock")
            End If
        End Sub
    
        Public Sub SysSuspend(ByVal sender As Object, ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
            If e.Mode = Microsoft.Win32.PowerModes.Resume Then
                MessageBox.Show("system is resuming")
            ElseIf e.Mode = Microsoft.Win32.PowerModes.Suspend Then
                MessageBox.Show("system being suspended")
            ElseIf e.Mode = Microsoft.Win32.PowerModes.StatusChange Then
                MessageBox.Show("system has a status change")
            End If
        End Sub

  15. #15
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Check if PC is in sleep mode

    Quote Originally Posted by HanneSThEGreaT View Post
    David, you said it may not be possible. I provided a decent way to do it, and how it should be implemented. So, what is the / your problem ¿
    It's always a loaded question, when you post IMPOSSIBLE in any public venue. Someone will prove you wrong 99% of the time. But, that's how we learn.

    It's not about who's right or wrong, either...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured