Solved

Using SetWaitableTimer

Posted on 1998-10-21
6
1,674 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 

Author Comment

by:gtanoni
ID: 1175711
Edited text of question
0
 

Author Comment

by:gtanoni
ID: 1175712
Edited text of question
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1175713
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1175714
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
ID: 1175715
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
ID: 1175716
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

695 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