Set Time Notification

I am writing a VC++ program for NT 4.0+. I am trying to have my program run all the time. However, it needs to be signalled at a certain time based on the INI entry. Say 18:00 in the evening on certain days. I would like my program to Wait for the time to pass by before it starts any processing or consume CPU time. I have used WaitforSingleObject before but I am not sure if I can signal this API at a certain time.
Is there an Function which allows me to do that. SOme code will be appreciated too.
rianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You can simply use 'Sleep()' for this task - more to follow...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
Or if the application has a user intereface, create a timer with SetTimer() and you will receive WM_TIMER messages periodically.  Check the time when you get the message to see if it is time to perform your processing.
0
jkrCommented:
The only difficulty is to compute the amount of milliseconds as the argument for sleep, you could use something like that:

#include <windows.h>
#include <time.h>
#include <stdio.h>

void main( void )
{
   struct tm when;
   time_t now, result;
   int    hour;
   DWORD  dwSleepTime;

   time( &now );
   when = *localtime( &now );
   printf( "Current time is %s\n", asctime( &when ) );
   printf( "Which hour to execute: " );
   scanf( "%d", &hour );

   when.tm_hour +=  hour;

   printf( "Will execute at %s\n", asctime( &when ) );

   if( (result = mktime( &when )) != (time_t)-1 )
   {
     // 'result' holds the time to execute in seconds since Jan 1st 1970
     // 'now' holds the actual time in seconds since Jan 1st 1970

     dwSleepTime = ( DWORD) ( result - now) * 1000;

     Sleep ( dwSleepTime);

     printf ( "\nWAKING UP!!\n");

   }
   else
      perror( "mktime failed" );

}

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jkrCommented:
nietod is right - but the method to compute the timer 'interval' is the same as shown above ;-)

It'd be quite similar (just forget about 'printf()' and friends):


   if( (result = mktime( &when )) != (time_t)-1 )
   {
     // 'result' holds the time to execute in seconds since Jan 1st 1970
     // 'now' holds the actual time in seconds since Jan 1st 1970

     dwSleepTime = ( DWORD) ( result - now) * 1000;

     SetTimer ( hMyWnd, 0, dwSleepTime, NULL);
   }

Or, alternatively

     SetTimer ( NULL, unMyID, dwSleepTime, MyTimerProc);
0
jkrCommented:
One thing I forgot to mention - 'Sleep()' only works < 49 days (DWORD overflow <-> milliseconds ;-)
0
ufolk123Commented:
Alternatively you can use NetScheduleJobAdd function(). The NetScheduleJobAdd function submits a program to run at a specified future time and date. You can also use this run a program at a remote system.

You can pass the program name as a comand in AT_INFO structure.which is a paparameter to NetScheduleJobAdd() function.

Additionally you will have control if you want your program to run on a periodic basic also.


NET_API_STATUS NetScheduleJobAdd(
LPWSTR Servername,       
LPBYTE Buffer,       
LPDWORD JobId       
);       


Buffer is pointer to strcuture of type
typedef struct _AT_INFO {
    DWORD   JobTime;
    DWORD   DaysOfMonth;
    UCHAR   DaysOfWeek;
    UCHAR   Flags;
    LPWSTR  Command;
} AT_INFO, *PAT_INFO, *LPAT_INFO;
 
This way you may not be bound with functionality problems of Sleep() etc (49.7 days limit).
Please see MSDN help for more details.


Hope this helps to acheive your goal.
Ufolk123
0
rianAuthor Commented:
I appreciate all the help. However, I was trying to find a way where I could use a function similar to WaitforSingleObject or MultipleObject.

Thanks a lot..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.