home *** CD-ROM | disk | FTP | other *** search
Wrap
VERSION 5.00 Begin VB.Form frmSleep Caption = "Sleeper" ClientHeight = 1455 ClientLeft = 60 ClientTop = 345 ClientWidth = 4395 LinkTopic = "Form1" ScaleHeight = 1455 ScaleWidth = 4395 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdSleep Caption = "GoodNight" Height = 465 Left = 1350 TabIndex = 2 Top = 720 Width = 1545 End Begin VB.TextBox txtSeconds Height = 375 Left = 2160 TabIndex = 0 Text = "15" Top = 180 Width = 1725 End Begin VB.Label Label1 Alignment = 1 'Right Justify Caption = "How Long Should I Sleep?" Height = 285 Left = 180 TabIndex = 1 Top = 270 Width = 1905 End Attribute VB_Name = "frmSleep" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit ' Copyright 1997 by Desaware Inc. All Rights Reserved Private Declare Function CreateWaitableTimer Lib "kernel32" Alias "CreateWaitableTimerA" (ByVal lpSemaphoreAttributes As Long, ByVal bManualReset As Long, ByVal lpName As String) As Long Private Declare Function OpenWaitableTimer Lib "kernel32" Alias "OpenWaitableTimerA" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal lpName As String) As Long Private Declare Function SetWaitableTimer Lib "kernel32" (ByVal hTimer As Long, lpDueTime As FILETIME, ByVal lPeriod As Long, ByVal pfnCompletionRoutine As Long, ByVal lpArgToCompletionRoutine As Long, ByVal fResume As Long) As Long Private Declare Function CancelWaitableTimer Lib "kernel32" (ByVal hTimer As Long) Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Const ERROR_ALREADY_EXISTS = 183& Private Const WAIT_OBJECT_0 = 0 Private Const WAIT_TIMEOUT = &H102& Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Dim TimerObjectHandle& Private Sub cmdSleep_Click() GoToSleep False End Sub Private Sub Form_Load() Dim res& Dim ft As FILETIME TimerObjectHandle = CreateWaitableTimer(0, True, "Ch14SleeperTimer") If Err.LastDllError = ERROR_ALREADY_EXISTS Then ' Another app already created the timer, ' we may need to go to sleep now GoToSleep True Else ft.dwLowDateTime = -1 ft.dwHighDateTime = -1 res = SetWaitableTimer(TimerObjectHandle, ft, 0, 0, 0, 0) End If End Sub ' Function to go to sleep. ' Slightly different behavior if called during form load Public Sub GoToSleep(ByVal IsLoading As Boolean) Dim ft As FILETIME Dim IsBusy As Long Dim delay As Double Dim delaylow As Double Dim Unit32 As Double Dim res& ' Is object signaled now? IsBusy = WaitForSingleObject(TimerObjectHandle, 0) ' We know WAIT_OBJECT_0 is 0, so False indicates signaled If IsLoading Then If IsBusy Then MsgBox "Hey! Everyone else is asleep already! I'm going to join them!", vbOKOnly, "I'm Tired" Else ' It's not active Exit Sub End If End If ' Update in case state changed during message box IsBusy = WaitForSingleObject(TimerObjectHandle, 0) If Not IsBusy Then ' Set the timer Unit32 = CDbl(&H10000) * CDbl(&H10000) delay = CDbl(txtSeconds.Text) delay = delay * 1000 * 10000 ' Delay is now 100ns ft.dwHighDateTime = -CLng(delay / Unit32) - 1 delaylow = -Unit32 * (delay / Unit32 - Fix(delay / Unit32)) If delaylow < CDbl(&H80000000) Then delaylow = Unit32 + delaylow End If ft.dwLowDateTime = CLng(delaylow) res = SetWaitableTimer(TimerObjectHandle, ft, 0, 0, 0, 0) End If Me.Hide App.TaskVisible = False IsBusy = WaitForSingleObject(TimerObjectHandle, -1) frmMorning.Move Me.Left, Me.Top frmMorning.Show vbModal Me.Show App.TaskVisible = True End Sub ' Close the handle on unload Private Sub Form_Unload(Cancel As Integer) Call CloseHandle(TimerObjectHandle) End Sub