How to trigger an event at a specified time
-
Hi, I'd like to trigger an event within my application at a specific time. Is there a better way other than installing a timer with SetTimer() and check the current time every 1 second? Thanks in advance. Have no fear of perfection - You will never reach it
-
Hi, I'd like to trigger an event within my application at a specific time. Is there a better way other than installing a timer with SetTimer() and check the current time every 1 second? Thanks in advance. Have no fear of perfection - You will never reach it
Take a look at CreateWaitableTimer and SetWaitableTimer. Below is the example from MSDN. Create a Thread to monitor for your event and then use SetWaitableTimer. When the the timer is signalled you can do what you need. SetWaitableTimer can either be set for an absolute time or a relative time(as in the example). If you use absolute time pay attention to your time local time offset. #include #include int main() { HANDLE hTimer = NULL; LARGE_INTEGER liDueTime; liDueTime.QuadPart=-100000000; // Create a waitable timer. hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer"); if (!hTimer) { printf("CreateWaitableTimer failed (%d)\n", GetLastError()); return 1; } printf("Waiting for 10 seconds...\n"); // Set a timer to wait for 10 seconds. if (!SetWaitableTimer( hTimer, &liDueTime, 0, NULL, NULL, 0)) { printf("SetWaitableTimer failed (%d)\n", GetLastError()); return 2; } // Wait for the timer. if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0) printf("WaitForSingleObject failed (%d)\n", GetLastError()); else printf("Timer was signaled.\n"); return 0; }
-
Take a look at CreateWaitableTimer and SetWaitableTimer. Below is the example from MSDN. Create a Thread to monitor for your event and then use SetWaitableTimer. When the the timer is signalled you can do what you need. SetWaitableTimer can either be set for an absolute time or a relative time(as in the example). If you use absolute time pay attention to your time local time offset. #include #include int main() { HANDLE hTimer = NULL; LARGE_INTEGER liDueTime; liDueTime.QuadPart=-100000000; // Create a waitable timer. hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer"); if (!hTimer) { printf("CreateWaitableTimer failed (%d)\n", GetLastError()); return 1; } printf("Waiting for 10 seconds...\n"); // Set a timer to wait for 10 seconds. if (!SetWaitableTimer( hTimer, &liDueTime, 0, NULL, NULL, 0)) { printf("SetWaitableTimer failed (%d)\n", GetLastError()); return 2; } // Wait for the timer. if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0) printf("WaitForSingleObject failed (%d)\n", GetLastError()); else printf("Timer was signaled.\n"); return 0; }
Thanks for the pointer, I've found the relative articles in msdn, but I can't see how these functions can be used in an MFC Doc/View context. In the other example in msdn, the completion routine used is a global function. My application needs to set the trigger and go about doing other things. It cannot halt until the event is triggered. It ought to be something very easy to do, but it seems very complicated to me. Have no fear of perfection - You will never reach it
-
Thanks for the pointer, I've found the relative articles in msdn, but I can't see how these functions can be used in an MFC Doc/View context. In the other example in msdn, the completion routine used is a global function. My application needs to set the trigger and go about doing other things. It cannot halt until the event is triggered. It ought to be something very easy to do, but it seems very complicated to me. Have no fear of perfection - You will never reach it
I am not very familiar with Doc/View. So hopefully you can apply the following as appropriate. What I normally do is create a thread, often times in the class constructor, that passes a this pointer as the thread parameter. The thread then calls back to the function that does the waitable timer. When the waitable timer completes it can set your trigger. A simple illustration... class myclass { myclass(); long settrigger(); static DWORD WINAPI waitthread(); long waittimer(); }; myclass::myclass() { HANDLE hThread(NULL); DWORD dwThreadID; hThread = (HANDLE)_beginthreadex(NULL, 0, waitthread, (LPVOID)this, 0, &dwThreadID); } DWORD WINAPI myclass::waitthread(LPVOID pParam) { myclass* ptr = static_cast(pParam); return(ptr->waittimer()); } long myclass::waittimer() { /* Do all your work with waitable timer. */ /* If everything is good! */ settrigger(); return 0; } long myclass::settrigger() { /* Set your trigger */ return 0; }