Solved

Using SetWaitableTimer

Posted on 1998-10-21
6
1,613 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
Industry Leaders: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 428
Find Visual Studio Tools 2 121
gdb doesn't stop on breakpoint 2 113
How to Correctly derive class from CWinThread in MFC 4 89
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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