From: shaziya on
Hi there,

MyApp creates a thread. And that thread uses CreateWaitableTime,
SetWaitableTimer, WaitForSingleObject.

But the problem my app is facing is if I go for the following code

VOID CALLBACK TimerProc(LPVOID lpArgToCompletionRoutine, DWORD
dwTimerLowValue, DWORD dwTimerHighValue)
{
//my code here;
}

//followincode is regarding creatim timer

LARGE_INTEGER liDueTime;
liDueTime.QuadPart= -600000000;
// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, _T("WaitableTimer"));
if (!hTimer)
return FALSE ;

if (!::SetWaitableTimer(hTimer, &liDueTime, 0, TimerProc, NULL, 0))

return FALSE ;

for (int i = 0; i < 10; i++)
SleepEx(INFINITE, TRUE);


Then it works well. After 60 seconds my TimerProc gets called. But I
dont want to use SleepEx function to have my thread in alertable state.
I want to use WaitFor SingleObject.

But if I use WaitForSingleObject instead of SleepEx then my TimerProc
doesn't get called.
Do i have to use QueueUserAPC. I dont think so bcoz SleepEx is working
without QueueUserAPC. So I think WaitForSingleObject should alos work.

So can Anybody tell me what is going on. Where I am doing any mistake.

Waiting for Quick reply.

Thanking in advance....

From: crisgoogle on
shaziya wrote:
> Hi there,

<snip>

> LARGE_INTEGER liDueTime;
> liDueTime.QuadPart= -600000000;
> // Create a waitable timer.
> hTimer = CreateWaitableTimer(NULL, TRUE, _T("WaitableTimer"));
> if (!hTimer)
> return FALSE ;
>
> if (!::SetWaitableTimer(hTimer, &liDueTime, 0, TimerProc, NULL, 0))
>
> return FALSE ;
>
> for (int i = 0; i < 10; i++)
> SleepEx(INFINITE, TRUE);
>
>
> Then it works well. After 60 seconds my TimerProc gets called. But I
> dont want to use SleepEx function to have my thread in alertable state.
> I want to use WaitFor SingleObject.
>
> But if I use WaitForSingleObject instead of SleepEx then my TimerProc
> doesn't get called.
> Do i have to use QueueUserAPC. I dont think so bcoz SleepEx is working
> without QueueUserAPC. So I think WaitForSingleObject should alos work.

>From MSDN on SetWaitableTimer:
"... and the thread that set the timer calls the completion routine
when it enters an alertable wait state."

SleepEx allows you to specify that the the thread is in an alertable
wait state, by setting the bAlertable parameter to TRUE, as you've
obviously discovered.

The cunningly obfuscated WaitForSingleObjectEx allows you to do the
same thing. I think that using WFSOEx( ..., ... , TRUE) instead of
plain vanilla WFSO should fix your problem.

Incidentally, QueueUserAPC wouldn't solve your problem either, as you
*still* have to enter an alertable wait state for it to run, and you
can't do so using plain WFSO.

Cris