Zdravim a po delsim case mam opet tema. Nasel jsem na internetu tridu WakeUp, z nejakého duvodu ale nefunguje, pripadne ji nespravne pouzivam. Pokud nekdo zna duvod, nebo pouziva jiny zpusob, poradte. Taky jeste zustava nedoreseno, jak uspany pocitac vzbudit vcetne prihlaseni konkretniho uzivatele. Predem diky. WakeUP:
Imports System.Text
Imports System.Runtime.InteropServices
Imports Microsoft.Win32.SafeHandles
Imports System.ComponentModel
Imports System.Threading
Namespace WakeUPTimer
Class WakeUP
<DllImport("kernel32.dll")> _
Public Shared Function CreateWaitableTimer(ByVal lpTimerAttributes As IntPtr, ByVal bManualReset As Boolean, ByVal lpTimerName As String) As SafeWaitHandle
End Function
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function SetWaitableTimer(ByVal hTimer As SafeWaitHandle, <[In]()> ByRef pDueTime As Long, ByVal lPeriod As Integer, ByVal pfnCompletionRoutine As IntPtr, ByVal lpArgToCompletionRoutine As IntPtr, ByVal fResume As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
Public Event Woken As EventHandler
Private bgWorker As New BackgroundWorker()
Public Sub New()
AddHandler bgWorker.DoWork, New DoWorkEventHandler(AddressOf bgWorker_DoWork)
AddHandler bgWorker.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf bgWorker_RunWorkerCompleted)
End Sub
Public Sub SetWakeUpTime(ByVal time As DateTime) '<<-----
Try
MsgBox(time)
If bgWorker.IsBusy Then
bgWorker.CancelAsync()
End If
bgWorker.RunWorkerAsync(time.ToFileTime())
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub bgWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
RaiseEvent Woken(Me, New EventArgs())
End Sub
Private Sub bgWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
Dim waketime As Long = CLng(e.Argument)
Using handle As SafeWaitHandle = CreateWaitableTimer(IntPtr.Zero, True, Me.[GetType]().Assembly.GetName().Name.ToString() & "Timer")
If SetWaitableTimer(handle, waketime, 0, IntPtr.Zero, IntPtr.Zero, True) Then
Using wh As New EventWaitHandle(False, EventResetMode.AutoReset)
wh.SafeWaitHandle = handle
wh.WaitOne()
End Using
Else
Throw New Win32Exception(Marshal.GetLastWin32Error())
End If
End Using
End Sub
Private Sub WakeUP_Woken(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Woken
'The Pc has woken, What Now?
'Run some code or call form1.AfterSleep etc...
End Sub
Public Function UnSetWakeUptime()
SetWakeUpTime("2669/01/01 12:00:00")
Return True
End Function
End Class
End Namespace
V udalosti tlacitka pak zkousim:
Dim Sleepy As New WakeUPTimer.WakeUP
Sleepy.SetWakeUpTime(Format(DateTime1.Value, "dd.MM.yyyy hh:mm"))
' Sleepy.SetWakeUpTime(DateTime1.Value)
Application.SetSuspendState(PowerState.Suspend, False, False)
Zkousel jsem predat i neformatovanou hodnotu DateTime1.value, chova se stejne - prejde do SUSPEND stavu, ale neprobudi se.
|