?
Solved

Using SetWaitableTimer

Posted on 1998-10-21
6
Medium Priority
?
1,736 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Accepted Solution

by:
alexo earned 150 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.
Suggested Courses

801 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