473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 4503
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Carlos Kirkconnell | last post by:
I'm programming a multi - threaded application using C#. I have two questions regarding to the use of threads 1- I have a Hashtable that will have multiple writters and multiple readers. I used...
3
by: CMan | last post by:
Hi, We are currently trying to install .Net Framework v.1.1 on a server which already has v1.0. We are receiving the following error. Error 1704.An installation for Microsoft .NET Framework...
4
by: Guido Kraus | last post by:
Hi, I have a VB Windows Forms app that has a single form ('MainForm'). MainForm has a shared (C#: static) class variable that holds a reference to a newly created Thread. This thread does some...
0
by: Lee | last post by:
Hello. I am trying to start a process in a suspended state in a c# application. Unfortunately there seems to be no support for suspended process creation in the Process class. Does anyone...
1
by: Amratash | last post by:
I'm receiving the above mentioned error.The problem is: "I suspend one thread.Another thread is resuming the first thread.Then I added following code:Its in simple english if(first thread is not...
1
by: Andreas Müller | last post by:
Hi All, What I want to do, is to reuse a thread after it was supended. However, if the thread was suspended for a while and I reinvoke it using Thread.Resume(), an exception is thrown. If I look...
1
by: Brett | last post by:
Once a thread is put into suspended mode, how is it restart? Calling Thread.start() will throw an error. Thanks, Brett
2
by: Mark Denardo | last post by:
I'm trying to abort a suspended thread, but I get a ThreadStateException: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in mscorlib.dll Additional...
1
by: Tim | last post by:
Folks, I have 3 loosely linked problems which I am would appreciate feedback on. 1). T-Sql and Active directory roles. We want to be able to control access to data within a table based on a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.