Solved

Using SetWaitableTimer

Posted on 1998-10-21
6
1,477 Views
Last Modified: 2009-07-29
I am trying to use a one-shot timer, when I start the timer with the call SetWaitableTimer(timer, &DueTime, 0,func,NULL, false), DueTime.QuadPart = -10000, the timer starts ok but it expires periodically and I can not stop it with CancelWaitableTimer(). Also, I trap the timer expiration in the WaitForMultipleObjects() but the CALLBACK function is never executed automatically (I do reset the event after it is detected). Can Someone show me an example of how to start a one-shot timer for a duration of one second?. Thank You.
0
Comment
Question by:gtanoni
  • 4
6 Comments
 

Author Comment

by:gtanoni
Comment Utility
Edited text of question
0
 

Author Comment

by:gtanoni
Comment Utility
Edited text of question
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Are you using a waitable timer because you are using threads? If you are not using threads you should look at the multi-media timers (timeSetEvent and timeKillEvent funcitons)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
Comment Utility
First, you should create a manual reset timer thus:
    HAHDLE timer = CreateWaitableTimer(NULL, TRUE, NULL);

Then, start it thus:
    SetWaitableTimer(timer, &DueTime, 0, NULL, NULL, FALSE);

Note that a completion routine is not used (you either WaitForMultipleObjects() or use a completion routine, not both).  Also note the uppercase 'FALSE' (it is an int constant, not a bool).

Or, use a completion routhine thus:
    SetWaitableTimer(timer, &DueTime, 0, func, NULL, FALSE);

However, in order to allow the completion routhine to be executed by the thread, the thread *must* ne in an alertable wait state.  Entering an alertable wait state is done by calling one of the following APIs: MsgWaitForMultipleObjectsEx(), SignalObjectAndWait(), WaitForMultipleObjectsEx() or WaitForSingleObjectEx().  This state should be entered immediately after calling SetWaitableTimer().
0
 

Author Comment

by:gtanoni
Comment Utility
I have tried the manual reset option on the CreateWaitableTimer call but I was not able to reset the event with ResetEvent(g_hEvents[TIMEOUT_EVENT]). So I set the CreateWaitableTimer flag to auto-reset and then it works fine. Thank you Alex for your answer.
0
 

Author Comment

by:gtanoni
Comment Utility
rwilson: The reason why I need a waitable timer is because I am writing a service which implements a specific real-time protocol on a COM port and I do not want to implement a message pump (dispatch loop) that uses all the CPU. It is a multi-thread appplication.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now