Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to trigger an event at a specified time

How to trigger an event at a specified time

Scheduled Pinned Locked Moved C / C++ / MFC
databasetutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Salvador Dali
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • S Salvador Dali

      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

      C Offline
      C Offline
      calhuskerfan
      wrote on last edited by
      #2

      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; }

      S 1 Reply Last reply
      0
      • C calhuskerfan

        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; }

        S Offline
        S Offline
        Salvador Dali
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • S Salvador Dali

          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

          C Offline
          C Offline
          calhuskerfan
          wrote on last edited by
          #4

          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; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups