G
Guest
I'm working on a PIM program, which can function as an alarm clock. It needs
to be able to suspend the computer, and wake it up at a particular time.
As a test, I'm using a Waitable timer (through PInvoke'd Win32 calls) to
trigger an event, say, 30 seconds later. I create a timer, set its
properties, and then start a new thread to listen for the timer. However the
event triggers instantly, instead of waiting 30 seconds. Am I setting the
trigger time wrongly, or is something else the issue?
PInvoke Commands :
[DllImport("kernel32.dll")]
public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll")]
public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
pDueTime, int lPeriod, TimerAPCProc pfnCompletionRoutine, IntPtr
lpArgToCompletionRoutine, bool fResume);
[DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
public static extern Int32 WaitForSingleObject(IntPtr handle, Int32
milliseconds);
public static uint INFINITE = 0xFFFFFFFF;
The main code is :
private IntPtr handle;
private void shutdownToolStripMenuItem_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
dt.AddSeconds(30.0);
long duetime = dt.ToFileTime();
handle = Win32.PowerManagement.CreateWaitableTimer(IntPtr.Zero, true, "");
Win32.PowerManagement.SetWaitableTimer(handle, ref duetime, 0, null,
IntPtr.Zero, true);
Thread t = new Thread(new ThreadStart(this.NewThread));
t.Start();
}
private void NewThread()
{
int ret = Win32.PowerManagement.WaitForSingleObject(handle,
(int)Win32.PowerManagement.INFINITE);
MessageBox.Show("Wait object"); // ret = 0x00000000L here
}
Any help would be greatly appreciated.
Regards,
Chris
to be able to suspend the computer, and wake it up at a particular time.
As a test, I'm using a Waitable timer (through PInvoke'd Win32 calls) to
trigger an event, say, 30 seconds later. I create a timer, set its
properties, and then start a new thread to listen for the timer. However the
event triggers instantly, instead of waiting 30 seconds. Am I setting the
trigger time wrongly, or is something else the issue?
PInvoke Commands :
[DllImport("kernel32.dll")]
public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll")]
public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
pDueTime, int lPeriod, TimerAPCProc pfnCompletionRoutine, IntPtr
lpArgToCompletionRoutine, bool fResume);
[DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
public static extern Int32 WaitForSingleObject(IntPtr handle, Int32
milliseconds);
public static uint INFINITE = 0xFFFFFFFF;
The main code is :
private IntPtr handle;
private void shutdownToolStripMenuItem_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
dt.AddSeconds(30.0);
long duetime = dt.ToFileTime();
handle = Win32.PowerManagement.CreateWaitableTimer(IntPtr.Zero, true, "");
Win32.PowerManagement.SetWaitableTimer(handle, ref duetime, 0, null,
IntPtr.Zero, true);
Thread t = new Thread(new ThreadStart(this.NewThread));
t.Start();
}
private void NewThread()
{
int ret = Win32.PowerManagement.WaitForSingleObject(handle,
(int)Win32.PowerManagement.INFINITE);
MessageBox.Show("Wait object"); // ret = 0x00000000L here
}
Any help would be greatly appreciated.
Regards,
Chris