471,350 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Waking a suspended computer.

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.Z ero, 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
Dec 2 '05 #1
1 4435
Use a relative time, that is the time im multiples of 100nsec.

long duetime=-300000000; // wait 30 seconds to signal the handle.
Willy.

"Christopher Stott" <Christopher St***@discussions.microsoft.com> wrote in
message news:C6**********************************@microsof t.com...
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.Z ero, 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

Dec 2 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Carlos Kirkconnell | last post: by
4 posts views Thread by Guido Kraus | last post: by
1 post views Thread by Andreas Müller | last post: by
1 post views Thread by Brett | last post: by
2 posts views Thread by Mark Denardo | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.