Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using CreateTimerQueueTimer

278 views
Skip to first unread message

sandy84

unread,
Jan 17, 2007, 5:22:40 AM1/17/07
to
Hello all,
I want to create a Windows NT Service which will delete records on
timely interval. So I am using ATL wizard generated code by using
windows services option. In Run() function I want to call the function
which will delete the record on timely interval. So for that the code
is as follows:

HANDLE m_hStop;
m_hStop = ::CreateEvent(0, TRUE, FALSE, 0);

HANDLE hTimerQTimer;
CreateTimerQueueTimer(&hTimerQTimer, NULL, DeleteRecords, NULL, 1000,
60000, WT_EXECUTELONGFUNCTION);

if (::WaitForSingleObject (m_hStop, INFINITE) == WAIT_OBJECT_0)
{
::CloseHandle(m_hStop);
DeleteTimerQueueTimer(NULL, hTimerQTimer, NULL);
}

where DeleteRecords is the function which is callback function. I am
setting event m_hStop in Handler() function for stop and shutdown. Is
this the right implementation or a better mechanism is possible to
achieve this same thing? Also is it possible that the function
DeleteRecords is member function of some class? Because when I make it
a member function of some class then I get following error:

error C2664: 'CreateTimerQueueTimer' : cannot convert parameter 3 from
'void (void *,unsigned char)' to 'void (__stdcall *)(void *,unsigned
char)' None of the functions with this name in scope match the target
type

Curretly DeleteRecords is defined as:

VOID CALLBACK DeleteRecords(PVOID pvContext, BOOLEAN fTimeout)

Thank you,
Sandeep

Mubashir Khan

unread,
Jan 17, 2007, 6:22:38 AM1/17/07
to
i guess u remove the CALLBACK macro from your method defination ... as that
may be replaced by __stdcall*
"sandy84" <sande...@gmail.com> wrote in message
news:1169029360.8...@11g2000cwr.googlegroups.com...

Ben Voigt

unread,
Jan 17, 2007, 9:12:45 AM1/17/07
to

"Mubashir Khan" <mu...@yahoo.com> wrote in message
news:e0Vqfni...@TK2MSFTNGP06.phx.gbl...

Really, you should take the function address rather than letting the name
decay to a function pointer... it will make the error message clearer.

Tom Widmer [VC++ MVP]

unread,
Jan 17, 2007, 10:10:40 AM1/17/07
to
sandy84 wrote:
> Hello all,
> I want to create a Windows NT Service which will delete records on
> timely interval. So I am using ATL wizard generated code by using
> windows services option. In Run() function I want to call the function
> which will delete the record on timely interval. So for that the code
> is as follows:
>
> HANDLE m_hStop;
> m_hStop = ::CreateEvent(0, TRUE, FALSE, 0);
>
> HANDLE hTimerQTimer;
> CreateTimerQueueTimer(&hTimerQTimer, NULL, DeleteRecords, NULL, 1000,
> 60000, WT_EXECUTELONGFUNCTION);
>
> if (::WaitForSingleObject (m_hStop, INFINITE) == WAIT_OBJECT_0)
> {
> ::CloseHandle(m_hStop);
> DeleteTimerQueueTimer(NULL, hTimerQTimer, NULL);

I'd pass INVALID_HANDLE_VALUE as the third parameter, to ensure your
callback has finished before you return.

> }
>
> where DeleteRecords is the function which is callback function. I am
> setting event m_hStop in Handler() function for stop and shutdown. Is
> this the right implementation or a better mechanism is possible to
> achieve this same thing?

It would be simpler just to use a loop with
WaitForSingleObject(m_hStop, 60000)
and do away with the timer entirely.

Also is it possible that the function
> DeleteRecords is member function of some class?

No. The best you can do is pass a static member function, and then pass
"this" as the callback parameter (arg 4 I think). Then the static member
can either call a non-static member, or just do the work itself,
accessing member variables through the parameter (cast to a pointer to
your class's type of course).

Because when I make it
> a member function of some class then I get following error:
>
> error C2664: 'CreateTimerQueueTimer' : cannot convert parameter 3 from
> 'void (void *,unsigned char)' to 'void (__stdcall *)(void *,unsigned
> char)' None of the functions with this name in scope match the target
> type
>
> Curretly DeleteRecords is defined as:
>
> VOID CALLBACK DeleteRecords(PVOID pvContext, BOOLEAN fTimeout)

Change that to:
static VOID CALLBACK DeleteRecords(PVOID pvContext, BOOLEAN fTimeout)
{
MyClass* pMyClass = static_cast<MyClass*>(pvContext);
//etc.
}

Tom

sandy84

unread,
Jan 18, 2007, 6:16:26 AM1/18/07
to

Thanks for the reply..

But my requirement is that the function DeleteRecords has to be called
say after each 10min. The time after which this function has to be
called is yet to be decided. But the most possible value will be near
1hr. So will it be feasible to use WaitForSingleObject(m_hStop,
3600000) in a loop? Can you please provide some link to a example which
will be similar my requiremnt?

Sandeep

Tom Widmer [VC++ MVP]

unread,
Jan 18, 2007, 10:52:19 AM1/18/07
to
sandy84 wrote:

> Thanks for the reply..
>
> But my requirement is that the function DeleteRecords has to be called
> say after each 10min. The time after which this function has to be
> called is yet to be decided. But the most possible value will be near
> 1hr. So will it be feasible to use WaitForSingleObject(m_hStop,
> 3600000) in a loop?

Yes, there's no limit on the value AFAIK other than it must be <
INFINITE. It should also be immune to date/time adjustments.

Can you please provide some link to a example which
> will be similar my requiremnt?

The simplest example I've seen just used Sleep, but that makes it hard
to stop it in a timely and clean way.

Tom

0 new messages