CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2005
    Posts
    331

    Implementing timers

    Hi,

    I have a program that currently does reads/writes to a storage device indefinitely, until the user closes the program. I would like to have a timer that stops the read/write loop after x seconds. I'm wondering what's the best/easiest way to implement this? I'm not familiar with multi-threading programming but it seems like this could be 1 way?

    Thanks.

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Implementing timers

    Look at MSDN: Using Timers.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Feb 2005
    Posts
    331

    Re: Implementing timers

    Thanks. My program is a console program though, so the main loop is done in main(). I'm not sure what hwnd should be(NULL?), nor whether to set it up with a callback function or not.

    Also when I tried to use SetTimer() with some timer identifier ITD_TIMER1, it complains that it's unidentified. Do I have to declare it somewhere? The MSDN examples weren't very helpful as they didn't show how or where to do all this.

    Thanks again.

    Edit:
    I found this example code here for console programs, and I've tried it and it works as it claims. However, it's a while loop that simply keeps printing messages at a set time interval. Since it's a while loop, I can't do anything other than wait to print out the messages. What I want instead is for the timer to interrupt what I'm doing(a for loop that's reading/writing to a storage device) and then break out of the read/write loop. I can't figure out how to do this with timers?
    Last edited by galapogos; May 25th, 2009 at 02:01 AM.

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Implementing timers

    You should be able to use SetTimer and set it up using a callback function. When your callback function is called you can set a boolean variable to TRUE and check that value of that variable in your loop. When you detect it is true, abort your loop.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Feb 2005
    Posts
    331

    Re: Implementing timers

    You mean something like this?

    Code:
    bool flag = false;
    
    VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime) {
      flag = true;
    }
    
    int main(int argc, char *argv[], char *envp[]) {
      UINT TimerId = SetTimer(NULL, 0, 500, &TimerProc);
      if (!TimerId)
    	  cout << "SetTimer error" << endl;
    
      for (;;)
      {
        // Read/write here
        // ...
    
        if (flag == true)
          break;
      }
      KillTimer(NULL, TimerId);
      return 0;
    }
    Do I even need GetMessage()/DispatchMessage()?

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Implementing timers

    It turns out that even when using SetTimer with the callback parameter, you still need a message pump. It's also not recommended to use SetTimer in console application is seems.

    Try to use a multimedia timer instead, timeSetEvent.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Feb 2005
    Posts
    331

    Re: Implementing timers

    Thanks. I'm guessing I use timeSetEvent in the same way, i.e. the callback function sets a flag that the loop checks, and breaks out of if it's true?

    Seems like timeSetEvent is obsolete and superceded by CreateTimerQueueTimer too.

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Implementing timers

    Quote Originally Posted by galapogos View Post
    Thanks. I'm guessing I use timeSetEvent in the same way, i.e. the callback function sets a flag that the loop checks, and breaks out of if it's true?
    Yes.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  9. #9
    Join Date
    Feb 2005
    Posts
    331

    Re: Implementing timers

    Sorry, but is there any example code for this? I'm having trouble declaring my callback function. Not sure of the syntax.

    Thanks.

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Implementing timers

    The signature of your callback should be:
    Code:
    void (CALLBACK)(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
    Coming from http://msdn.microsoft.com/en-us/libr...23(VS.85).aspx
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  11. #11
    Join Date
    Feb 2005
    Posts
    331

    Re: Implementing timers

    Thanks. I ended up using CreateTimerQueueTimer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured