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?
 
jkrConnect With a Mentor Commented:
You can simply use 'Sleep()' for this task - more to follow...
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.