tshallenberger
tshallenberger

Reputation: 2846

How to react to an event triggered by a specific date and time being reached

I'm writing an application in C++ using Visual Studio in windows 7. The application is a single page MFC dialog application. I want to have a message box pop up when a certain time and date has been reached. I am using a Date Picker to obtain the specified date that will work as the trigger, and CTime objects to store the current time, and the expected time.

Unfortunately, I don't know where to start looking. Is there an object that sends notifications or messages to the window once a minute, on each minute? If not, is there a specific way I can go about having this accomplished, or suggestions as to where I may start looking?

Edit: Is it possible to calculate how many seconds there are between the current date and the future date and create a separate thread that counts down (with a timer)?

Upvotes: 1

Views: 1891

Answers (2)

Ajay
Ajay

Reputation: 18431

You can use SetTimer as suggested by @StackedCrooked. But that wont work if system time is changed. Say, for example, current time is 12:30, and you set it for 12:35, so the SetTimer you would set would be for 5 minutes. But, if system time is changed to 12:32, the timer would be triggered someway aroung 12:37, and not at 12:35 !

For this, you may like to use CreateWaitableTimer.

Upvotes: 0

StackedCrooked
StackedCrooked

Reputation: 35525

Here's a rough sketch of the functionality you need to implement to receive timer events:

UINT_PTR timerId = NULL;

void OnTimerEvent(HWND, UINT, UINT_PTR id, DWORD)
{
    if (timerId == id)
    {
        // timer action...
    }
}

void StartTimer()
{
    // call OnTimerEvent after 1000 milliseconds
    timerId = SetTimer(NULL, NULL, 1000, &OnTimerEvent);
}

void StopTimer()
{
    KillTimer(NULL, timerId);
}

Upvotes: 1

Related Questions