Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C#, wake computer from sleep

Name: Anonymous 2011-01-24 12:51

I'm creating a small alarm clock for fun, and I want to wake up the computer from sleep at a certain time.

I found this code, http://www.pcreview.co.uk/forums/showpost.php?p=11849260&postcount=4, and it works.
But.. as soon as I initiate it the program becomes unresponsive until the timer is finished.

What to do?

Name: Anonymous 2011-01-24 13:00

Do you actually understand the code?
There's no explicit "Wake up in X" function there; all the code does is it goes to sleep for a specific time, and I suppose that the P/Invoke calls are guaranteed to work even if the computer goes to sleep while the code sleeps.
Now, if the purpose of the program is to wake up your PC after a certain time, what the fuck would you want to do in the meantime?
Just spawn a background thread for the sleeping and it should work.

Name: Anonymous 2011-01-24 13:00

Do not run the program so it won't become unresponsive.

Name: Anonymous 2011-01-24 13:03

>>2
Not really, the code is way over my head.

Well, I want the GUI to be responsible until I put it to sleep, so I can cancel the alarm etc.

All right, thanks. I'll read up on threads.

Name: Anonymous 2011-01-24 13:11

>>2
P/Invoke is for calling native libs. It's basically FFI.

Name: Anonymous 2011-01-24 13:29

>>5
`implying I didn't know that
And your point?

Name: Anonymous 2011-01-24 13:32

>>6
`implying
I see what your problem is.
You're from that place.

Name: Anonymous 2011-01-24 13:35

>>7
`implying yo'ure board doesn't suck shit compared to the imageboards

Name: Anonymous 2011-01-24 13:36

>>7
`implying

Yes. I'm from the country of quote-capable people.

Name: Anonymous 2011-01-24 13:39

why not actually learn a programming language; to enable you to do this.

Or use a real alarm clock?

c# - please, excuse my giggling.

Name: VIPPER 2011-01-24 13:50

>>8
Then why dont you go back there if it is so much better?

Name: Anonymous 2011-01-24 13:54

ignore >>1-10 please go back to /g/

Name: Anonymous 2011-01-24 13:57

>>6
The point is P/Invoke has nothing to do with running code while the system is asleep. It's just (apparently, but I'm doubtful) the only way to setup a handler for the alarm, in this case by using kernel32.dll calls.

Name: Anonymous 2011-01-24 14:00

OP here, got it working by running the method in a new thread.
Thanks.

Name: Anonymous 2011-01-24 14:33

>>14
What happens if you press the alarm button twice? Spawn TWO threads?

Name: >>2 2011-01-24 14:39

>>13
What I meant was more of a
(...) I suppose that [those particular] P/Invoke calls [as opposed to the available .NET methods] are guaranteed to work (...)
There are a few ways to do a similar thing in .NET, but I thought that while the entire .NET runtime will be paused when the system goes to sleep (and so using, say, the Timer classes would be pointless), the OS functionality which you access through P/Invoke will still be available.

After reading a bit more, it seems that the CreateWaitableTimer(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) eventually causes WM_TIMER messages to be sent to the process/window message queue. The more you know (hosi)

Name: Anonymous 2011-01-24 14:55

>>16
I really lol'd.

Name: Anonymous 2011-01-24 20:40

>>16
Really nice.

Name: Anonymous 2011-01-25 0:19

>>16
Gotcha.

Name: Anonymous 2011-01-25 1:06

>>19
Cool.

Name: Anonymous 2011-01-25 6:57

http://www.pcreview.co.uk/forums/showpost.php?p=11849260&postcount=4
So.. why create two waitable timers in this example?
I fail to see the logic.

Name: Anonymous 2011-01-25 7:12

I'm sorry, he's in a coma.

Name: Anonymous 2011-02-04 17:00


Don't change these.
Name: Email:
Entire Thread Thread List