Menu

Run function every X minutes, like a timer.

2002-12-03
2012-09-26
  • Nobody/Anonymous

    I have a bunch of functions that i need to be tun every X minutes.
    Supose i set this X to 5, it would mean that each 5 minutes all the functions will have to run again.

    Is there some kind of a timer function on C libs or maybe at the winapi that i could use? Im searching for that with no success.

     
    • Anonymous

      Anonymous - 2002-12-03

      Check into SetTimer in the WIndows API docs.

      Dale

       
    • Nobody/Anonymous

      I had a look at what i could find on SetTimer and tell me if im getting it all wrong... if i want to display a text field on my screen like: The function X is beeing executed for HH:MM:SS this means that i would have to set the timeout for my timer to 1 sec and the wm_timer event would add 1 sec to my time string?

       
    • Anonymous

      Anonymous - 2002-12-04

      The SetTimer function is part of the Win API. It will
      send a WM_TIMER to your WinProc function at
      the intervals you set it to send them. What you do
      when the timer goes off is up to you.
      For example the following is from one of my programs. I initialize the timer, inside the WinProc,
      when the WM_CREATE message is sent to my
      prog:
      aTimer = SetTimer(hwnd, 1, lSpeed * 1000, NULL);

      aTimer is the handle for my timer, I'll use it to turn
      the timer off when the WM_DESTROY message
      is issued. hwnd is the handle of the window that
      Windows will send WM_TIMER messages to.
      lSpeed is a varible my program user sets, I multiply
      it by 1000 which converts it to seconds, approxamatly anyway.(Timer intervals are in
      milliseconds. The last parameter, the one I have
      set to NULL in the example could be pointed to
      a function in your program where WM_TIMER
      messages could be sent instead of to the WinProc
      function.(I've never used it that way)

      The following is an example of acting upon the
      WM_TIMER message.
              case WM_TIMER:
                  if (!LoadNextImage())
                    MessageBox(NULL,"Couldn't load BMP","ERROR!",MB_OK);
                  DrawNewScreen(hwnd);  //do your art work
                  break;

      Hope this is some help,

      Dale

       
    • Nobody/Anonymous

      Thanks Dale, that helped me a lot,  already made a timer that call the functions every 10 seconds.

      I was just wondering now, how could i make a edit field display in the screen the time in HH:MM:SS that the whole program is opened.
      I thought of creating a timer with a 1s interval and at this timer CASE, call a function to update the HH:MM:SS string and send a message to the text field. I was just wondering if you agree this is the right  way to do that, or am i wasting cpu cycles with that cause theres a more intelligent way to do that :)

      And thanks again for the help!

       
    • Nobody/Anonymous

      Yeah, you would probably answer me this, ill antecipate :)
      I did what i said at the post above and i found that its not the most precise way to show time :\

      Heres what i did:

      WM_CREATE
      ...
      TheTimer = SetTimer(mainWindow, 401, 1000, NULL);
      ...

      WM_TIMER:  {
            switch(LOWORD(wParam)) {
            case IDT_TIMER:             
              createTime();
              return 0;    
            }            
          }

      And at other part of main.c, a function:

      int createTime() {
      char ActiveTime[10];
      SS++;

      if (SS==60) {
          SS=0;
          MM++;
          }
         
      if (MM==60) {
          MM=0;
          HH++;
          }
             
      sprintf(ActiveTime, "%dh:%dm:%ds", HH, MM, SS);
      SendMessage(hwndActiveTimeEditField, WM_SETTEXT, 0, ActiveTime);
      return 0;
      }

      But looking at a real watch i can see that for every 'real' minute my function is displaying a delay of about 5 seconds and growing.

      Any ideas?

       
    • Anonymous

      Anonymous - 2002-12-05

      Hi,
      I've been logged off since last night. If you are tyring to display the time of day, like a clock, take
      a look at the Win API function GetSystemTime.
      You'll have to do some conversion but it will be
      much more accurate.

      Hope this helps,

      Dale

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.