Raise event at time...

  • Thread starter Thread starter Thomas
  • Start date Start date
T

Thomas

I wouldn't think twice about this on the desktop, but for Pocket PCs I
want to be a little more "performance" aware. Any ideas on raising
events at certain times? I'm looking to run a method at the top of
the hour, every hour if the app is running.

I realize I can use a timer to periodically look for this time.
However, I'm trying to think of a better, more performant way to do
this, since this timer would be called a lot unnecessarily while it
looks for minute/second zero.

Thanks,
Tom
 
You could look at the current time, figure out how many ms till you want the
event to happen and do a WaitForSingleObject() P/Invoke, specifying an event
that won't fire unless the application is being exited, and a timeout equal
to the time you want to wait. Something like this.

while ( 1 )
{
int msTillHour = <figure this out here>;
IntPtr eventSetWhenUserExitsProgram = <get the event which will be
set on app exit, so that this thread can be released>;

int result = WaitForSingleObject( eventSetWhenUserExitsProgram,
msTillHour );
if ( result == WAIT_TIMEOUT )
{
// Do whatever it is that you want to do on the hour.
}
else
{
// Exit the thread immediately. The application is exiting.
break;
}
}
 
Using CeRunAppAtTime or SetUserNotification to invoke something every hour
would be the direction I'd go. Probably launch an app that raises a system
event which your app could be listening for.
 
Hi Paul,

Out of curiosity, and kind of a newbie type question, but what would be the
difference between the code you provided, and instead of
WaitForSingleObject, just do a Thread.Sleep(totalms)? (Assuming this code is
executed in its own thread)...

Brian
 
The thread will not awaken until the sleep period is over. If that happens
to be 59 minutes, that's a little long for the user to wait before the
program *really* exits when he tries to close it. Waiting on an event
allows the thread to be exited gracefully (and early), instead.

Paul T.
 
Back
Top