Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Using SetWaitableTimer

Posted on 1998-10-21
6
Medium Priority
?
1,789 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

647 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