🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
13 messages
13 total messages Started by Wiljo Fri, 14 Sep 2007 21:02
Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1305
Author: Wiljo
Date: Fri, 14 Sep 2007 21:02
82 lines
3898 bytes
Hi,

Original question was in <language cpp>, but it was suggested to post 
this question here. So here it is again. Sorry for the inconvenience.

I am writing a PVCR program to schedule recording of TV shows, and I 
want the computer to allow the Standby mode between recordings. I am 
using waitable timers in order to wake up the computer before every new 
recording, but that doesn't happen. I can see that the waitable timer 
functions, because my program receives it's signal at the correct time, 
as long as the computer does not go into standby. But I want my computer 
to wake from standby too.

I have written the following code snippet, that I have found browsing 
the net, and adapted to my needs:

SYSTEMTIME sTimeCurrent, sTimeRecord;

GetLocalTime(&sTimeCurrent);
sTimeRecord = sTimeCurrent;
sTimeRecord.wMinute += 1; //Wake up after one minute for this test case
//Convert to a FILETIME
FILETIME fLocalTime;
SystemTimeToFileTime(&sTimeRecord,&fLocalTime);

//Convert from local time to UTC time
//since the system works on UTC internally.
FILETIME fTime;
LocalFileTimeToFileTime(&fLocalTime,&fTime);

//Put time into a LARGE_INTEGER structure
//for the SetWaitableTimer function
LARGE_INTEGER liDueTime;

liDueTime.HighPart = fTime.dwHighDateTime;
liDueTime.LowPart = fTime.dwLowDateTime;

//Create the WaitableTimer
HANDLE hTimer = CreateWaitableTimer(NULL,TRUE,"VideoTimer");

if( hTimer != NULL )
{
   CancelWaitableTimer( hTimer );
   //Set the timer with the required time. Set the last
   //parameter to true to wake up the computer if asleep
   if( SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, true ))
   {
     StandbyPanel->Caption = "Timer has been set!";

     //* This executes a thread that uses
     //* WaitForSingleObject( hTimer, 90000 );
     //* It either updates the StandbyPanel with the current DateTime
     //* or gives a failure message after the 90 seconds timeout
     mWaitingTest.mExecuteThread( new( HERE ) CWaitingThread( hTimer ));
     //* In this example the Panel shows the date time after one minute
     //* Thus the signal is received by the thread

     //* Set the system to standby and it should wake after 1 minute
     SleepEx( 1000, false );
     StandbyPanel->Caption = "Going to sleep now!";
     //* Here I initiate the Standby mode
     //* But when I enable this the computer does not wake at
     //* the desired time of one minute
//  SetSuspendState( FALSE, FALSE, FALSE );
   }
   else
   {
     StandbyPanel->Caption = "Failed to set Timer!";
   }
}
else
{
   StandbyPanel->Caption = "Failed to create Timer!";
}


What am I missing here? As far as I know this piece of code should work, 
but is there something else that has to be done to wake up the computer?

Thanks in advance.

Wiljo.
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1306
Author: Remy Lebeau (TeamB)
Date: Fri, 14 Sep 2007 22:04
25 lines
1738 bytes
"Wiljo" <nospam@nospam.com> wrote in message
news:46eaf724$1@newsgroups.borland.com...

> What am I missing here?

Once the computer enters Standby mode, no programs are running.  That is the
whole point of Standby mode - to suspend the computer in its current state
without turning off the power completely.  It is like a slimmed down
Hibernation.

> As far as I know this piece of code should work

Not in Standby mode.

> is there something else that has to be done to wake up the computer?

The only things (that I know of) that can wake up a computer from Standby
mode are the user, sending a "magic packet" to a NIC card that has its "wake
on LAN" setting enabled, or a USB device that has the "Allow this device to
bring the computer out of standby" setting enabled in the OS's Power
Management setup.


Gambit
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1307
Author: Bob Gonder
Date: Sat, 15 Sep 2007 00:03
15 lines
1759 bytes
Remy Lebeau (TeamB) wrote:

>Not in Standby mode.

Is there a seperate "Sleep" mode?
I think this help item is what he was trying to do...

System Wake-up Events
Your application can restore an On-Now capable computer that is in a sleeping state to the
working state by using a scheduled time or a device event. This is known as a wake-up
event. Use a waitable timer object to specify the time at which the system should wake. To
create the object, use the CreateWaitableTimer function. To set the timer, use the
SetWaitableTimer function. The pDueTime parameter specifies when the timer will be
signaled. To specify that the system should wake when the timer is signaled, set the
fResume parameter to TRUE.
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1308
Author: Bob Gonder
Date: Sat, 15 Sep 2007 00:03
39 lines
2460 bytes
Wiljo wrote:

>HANDLE hTimer = CreateWaitableTimer(NULL,TRUE,"VideoTimer");
>
>if( hTimer != NULL )
>{
>   CancelWaitableTimer( hTimer );

Redundant.
SetWaitableTimer cancels the timer first.

>   if( SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, true ))
To eliminate possible UTC problems, test with
    liDueTime.QuadPart=-200000000;	// 20 seconds
and see if it sleeps for 20 seconds.
>   {
Also, you should check this error as well....
fResume 
[in] If this parameter is TRUE, restores a system in suspended power conservation mode
when the timer state is set to signaled. Otherwise, the system is not restored. If the
system does not support a restore, the call succeeds, but GetLastError returns
ERROR_NOT_SUPPORTED. 

>     //* This executes a thread that uses
>     //* WaitForSingleObject( hTimer, 90000 );
>     mWaitingTest.mExecuteThread( new( HERE ) CWaitingThread( hTimer ));

I don't think hTimer is valid in another thread.
Your security attribute is set to "not-inheritable".

>     //* Here I initiate the Standby mode
>     //* But when I enable this the computer does not wake at
>     //* the desired time of one minute
>//  SetSuspendState( FALSE, FALSE, FALSE );

I think you should use a callback function in 
SetWaitableTimer. It would either finish the job of turning on
the power, or just post a message for your app to start recording.
It could also Delete the Timer so you could Create it the next time.
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1309
Author: Remy Lebeau (TeamB)
Date: Sat, 15 Sep 2007 00:26
13 lines
1368 bytes
"Bob Gonder" <notbg@notmindspring.invalid> wrote in message
news:u47me3ltdcd2btvkn32ib000kv676ljiun@4ax.com...

> I don't think hTimer is valid in another thread.

Yes, it is:

    "Any thread of the calling process can specify the timer object handle
in a call to one of the wait functions."


Gambit
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1312
Author: Wiljo
Date: Sat, 15 Sep 2007 17:51
27 lines
2477 bytes
Bob Gonder wrote:
> Remy Lebeau (TeamB) wrote:
> 
>> Not in Standby mode.
> 
> Is there a seperate "Sleep" mode?
> I think this help item is what he was trying to do...
> 
> System Wake-up Events
> Your application can restore an On-Now capable computer that is in a sleeping state to the
> working state by using a scheduled time or a device event. This is known as a wake-up
> event. Use a waitable timer object to specify the time at which the system should wake. To
> create the object, use the CreateWaitableTimer function. To set the timer, use the
> SetWaitableTimer function. The pDueTime parameter specifies when the timer will be
> signaled. To specify that the system should wake when the timer is signaled, set the
> fResume parameter to TRUE.
> 
> 
Thanks,
Yes this is exactly what I want. My computer is On-Now capable, because 
I have another application running, another VCR program, that does wake 
the computer at the correct time, to start recording. However that 
commercial program contains too many bugs, and crashes on a regular 
basis, that is why I am writing my own replacement. According to 
examples on the internet, what I have written should work. But I will 
look into it some more.
Wiljo.
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1311
Author: Wiljo
Date: Sat, 15 Sep 2007 17:56
35 lines
2631 bytes
Remy Lebeau (TeamB) wrote:
> "Wiljo" <nospam@nospam.com> wrote in message
> news:46eaf724$1@newsgroups.borland.com...
> 
>> What am I missing here?
> 
> Once the computer enters Standby mode, no programs are running.  That is the
> whole point of Standby mode - to suspend the computer in its current state
> without turning off the power completely.  It is like a slimmed down
> Hibernation.
> 
>> As far as I know this piece of code should work
> 
> Not in Standby mode.
Of course no programs are running Standby mode. That is why I am using 
waitable timers in the first place. They are supposed to return the 
computer to the active state at the specified time. That is what 
waitable timers are all about. It just doesn't work in my case.

> 
>> is there something else that has to be done to wake up the computer?
> 
> The only things (that I know of) that can wake up a computer from Standby
> mode are the user, sending a "magic packet" to a NIC card that has its "wake
> on LAN" setting enabled, or a USB device that has the "Allow this device to
> bring the computer out of standby" setting enabled in the OS's Power
> Management setup.
> 
Well that is simply not true. Other applications, another PVR that I am 
still using, does wake the computer at the right time to start a 
recording. No user input needed to do that!
> 
> Gambit
> 
> 
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1313
Author: Wiljo
Date: Sat, 15 Sep 2007 18:08
57 lines
3508 bytes
Bob Gonder wrote:
> Wiljo wrote:
> 
>> HANDLE hTimer = CreateWaitableTimer(NULL,TRUE,"VideoTimer");
>>
>> if( hTimer != NULL )
>> {
>>   CancelWaitableTimer( hTimer );
> 
> Redundant.
> SetWaitableTimer cancels the timer first.
I saw this in another example and I thought that too, but I figured that 
it can't hurt, because I am setting the timer right thereafter.
> 
>>   if( SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, true ))
> To eliminate possible UTC problems, test with
>     liDueTime.QuadPart=-200000000;	// 20 seconds
> and see if it sleeps for 20 seconds.
I don't have UTC problems, because the thread I created uses the 
WaitForSingleObject function, with the handle to that timer, followed by 
an update of the caption of a panel on the main window at the correct 
time. So the thread works, as long as I don't suspend my computer. The 
signal of the waitable timer arrives at the correct time.
>>   {
> Also, you should check this error as well....
> fResume 
> [in] If this parameter is TRUE, restores a system in suspended power conservation mode
> when the timer state is set to signaled. Otherwise, the system is not restored. If the
> system does not support a restore, the call succeeds, but GetLastError returns
> ERROR_NOT_SUPPORTED. 
I'll try this. I didn't know that. You would expect a function to return 
TRUE on every success, not on half successes.
> 
>>     //* This executes a thread that uses
>>     //* WaitForSingleObject( hTimer, 90000 );
>>     mWaitingTest.mExecuteThread( new( HERE ) CWaitingThread( hTimer ));
> 
> I don't think hTimer is valid in another thread.
> Your security attribute is set to "not-inheritable".
The handle is valid in another process, because my example works as long 
as the computer is not sent to the suspended state before the timer signals.
> 
>>     //* Here I initiate the Standby mode
>>     //* But when I enable this the computer does not wake at
>>     //* the desired time of one minute
>> //  SetSuspendState( FALSE, FALSE, FALSE );
> 
> I think you should use a callback function in 
> SetWaitableTimer. It would either finish the job of turning on
> the power, or just post a message for your app to start recording.
> It could also Delete the Timer so you could Create it the next time.
> 
> 
That is a good idea, and I will let you know if that does the trick.

Thanks,
Wiljo.
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1314
Author: Remy Lebeau (TeamB)
Date: Sun, 16 Sep 2007 03:48
25 lines
2111 bytes
"Wiljo" <nospam@nospam.com> wrote in message
news:46ec1d00@newsgroups.borland.com...

> That is what waitable timers are all about.

That is not what waitable timers are all about.  The feature you are trying
to use is just a small piece of what they are primarily meant for - an event
object that can be waited on via the various Msg/WaitFor...() functions,
that is automatically signalled at a specific time by the OS instead of the
application.  Waking up the machine from Standby is a secondary effect.  The
wakeup feature is barely mentioned in the several pages of documentation
Microsoft has on what waitable timers are and how to use them.

> It just doesn't work in my case.

I tested the exact code you provided earlier, and it works as-is for me on
my XP laptop without any problem.  When entering Standby mode, the display
and all of the lights turn off.  Exactly 1 minute later, all of the lights
turn back on (but not the display, as per MSDN's documentation) and the
program's UI has been updated with the correct timestamp.  So I'm guessing
that the problem is with your machine/OS, not your code.


Gambit
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1321
Author: << Wiljo >>
Date: Mon, 17 Sep 2007 11:52
44 lines
3412 bytes
Remy Lebeau (TeamB) schreef:
> "Wiljo" <nospam@nospam.com> wrote in message
> news:46ec1d00@newsgroups.borland.com...
> 
>> That is what waitable timers are all about.
> 
> That is not what waitable timers are all about.  The feature you are trying
> to use is just a small piece of what they are primarily meant for - an event
> object that can be waited on via the various Msg/WaitFor...() functions,
> that is automatically signalled at a specific time by the OS instead of the
> application.  Waking up the machine from Standby is a secondary effect.  The
> wakeup feature is barely mentioned in the several pages of documentation
> Microsoft has on what waitable timers are and how to use them.
> 
Sorry I wrote that waitable timers are all about waking up one's 
computer. It clearly does a lot more, and waking up is just a side 
effect. Is there maybe another procedure to wake up the computer at a 
cerain time? I don't want to set up a wait procedure that uses a 
WaitForSingleObject or something, because it is more difficult to change 
the wakeup time later. There should be a simpler way of setting up a 
timer, that wakes the computer, if it went to sleep, at a certain time.
>> It just doesn't work in my case.
> 
> I tested the exact code you provided earlier, and it works as-is for me on
> my XP laptop without any problem.  When entering Standby mode, the display
> and all of the lights turn off.  Exactly 1 minute later, all of the lights
> turn back on (but not the display, as per MSDN's documentation) and the
> program's UI has been updated with the correct timestamp.  So I'm guessing
> that the problem is with your machine/OS, not your code.
> 
I am glad to read that my piece of code does work on your machine, but I 
am not sure if this problem is machine/OS related. I have another 
commercial PVR running, that does wake my computer at the correct time 
to record a show. That program "ShowShifter" is not stable enough for my 
liking, and the filename convention is not ideal. So now I am using 
ATV2000 and writing my own scheduler. This scheduler is already working 
fine with ATV2000, but does not yet take care of sleeping and waking up. 
And I would like to do that as well. Leaving my computer on the whole 
day is not very environmently friendly.
> 
> Gambit
> 
> 
Wiljo.
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1322
Author: Remy Lebeau (TeamB)
Date: Mon, 17 Sep 2007 17:32
30 lines
2310 bytes
"<< Wiljo >>" <wherever@theuniverse.com> wrote in message
news:46ee6a93$1@newsgroups.borland.com...

> Is there maybe another procedure to wake up the computer at a
> cerain time?

Not that I know of.

> I don't want to set up a wait procedure that uses a WaitForSingleObject
> or something, because it is more difficult to change the wakeup time
later.

Not really.  You can create a separate event for that using CreatEvent(),
then you use WaitForMultipleObjects() instead of WaitForSingleObject().
That way, when you want to change a timer that is already in progress,
signal the event so the waiting stops immediately.  The code can then test
which object was signalled.  If the event was siangled then cancel and reset
the timer as needed.

> I am glad to read that my piece of code does work on your machine, but I
> am not sure if this problem is machine/OS related.

All I know is that the code you have shown works fine in a simple test app.
If it is not related to the machine/OS, then there is probably something
else in your program is that effecting power states.  Maybe you are not
responding to a power notification correctly or something, I don't know.


Gambit
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1324
Author: << Wiljo >>
Date: Tue, 18 Sep 2007 10:51
19 lines
2247 bytes
Remy Lebeau (TeamB) schreef:
> All I know is that the code you have shown works fine in a simple test app.
> If it is not related to the machine/OS, then there is probably something
> else in your program is that effecting power states.  Maybe you are not
> responding to a power notification correctly or something, I don't know.
> 
As fas as I know, my program is not effecting power states somewhere 
else. The only thing missing maybe, are these power notification 
messages that I am not responding to. Could this be the problem?
I did see something about these messages in documentation I found, but 
was not aware that I have to deal with them too. All changes I make to 
the recording schedule are saved to disk immediately. So my scheduler 
program does not need to worry about the system going to sleep (standby 
or hibernation for that matter), because all it's settings are already 
saved. Thus I thought I don't need to bother with them. Do you know 
which message in particular I need to respond to, and in which way?
Any help here would be much appreciated!
Many thanks so far.
Wiljo.
Re: Can't get the computer to programmatically wake from standby.... (original question in language cpp)
#1331
Author: Remy Lebeau (TeamB)
Date: Tue, 18 Sep 2007 17:40
10 lines
1416 bytes
"<< Wiljo >>" <wherever@theuniverse.com> wrote in message
news:46efad93$1@newsgroups.borland.com...

> Do you know which message in particular I need to respond to

WM_POWERBROADCAST


Gambit
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads