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?
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?
dglienna
July 27th, 2009, 05:40 PM
I don't think you can detect when it wakes up. Any code that you write will be in the sleep state anyways
sonia.sardana
July 27th, 2009, 09:18 PM
ok it it possible to detect it..when PC goes to sleep mode.
dglienna
July 28th, 2009, 01:10 AM
Yes. Just not the opposite
HanneSThEGreaT
July 28th, 2009, 01:41 AM
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 :
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 :)
dglienna
July 28th, 2009, 02:13 AM
He wants to know WHEN it wakes up.
dglienna
July 28th, 2009, 02:14 AM
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+)
HanneSThEGreaT
July 28th, 2009, 03:28 AM
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 ¿
HanneSThEGreaT
July 28th, 2009, 04:27 AM
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. :)
TT(n)
July 28th, 2009, 06:07 AM
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.
I don't have a sample, but if you guys agree that it should work, I'll try and make a subclass example.
sonia.sardana
July 28th, 2009, 06:20 AM
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
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
TT(n)
July 28th, 2009, 06:39 AM
Works like a charm, without API!
:thumb:
This helped me.
Added to your reputation
HanneSThEGreaT
July 28th, 2009, 06:54 AM
Very nice work indeed :thumb:
I also learnt something new today! :)
Also, added to your reputation :)
TT(n)
July 28th, 2009, 07:24 AM
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.:
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
dglienna
July 28th, 2009, 05:45 PM
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...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.