Reputation: 11
I'm new to C and trying to find some code snippets to figure out how time triggered tasks can be implemented in C. I have two functions whose execution times may vary from 50 to 200 ms. I want to pass these functions to a worker thread which should be scheduled to run every 500 ms. Is there in C (win32-platform) a simple way (like java's TimerTask) to implement timer tasks with standard run time libraries?
Upvotes: 1
Views: 362
Reputation: 588
Take a look at the TimerQueue
related functions:
http://msdn.microsoft.com/en-us/library/ms682483(v=vs.85).aspx
Upvotes: 0
Reputation: 68561
Use CreateTimerQueueTimer
to have windows call your function every 500ms:
void CALLBACK timer_function(void* /*lpParameter*/,BOOLEAN /*TimerOrWaitFired*/)
{
/* do stuff */
}
HANDLE timer_handle;
void start_timer()
{
void* parameter; /* passed as lpParameter of timer_function */
DWORD milliseconds_before_first_call=100; /* execute after 100ms */
DWORD milliseconds_between_calls=500; /* and then every 500ms */
CreateTimerQueueTimer(&timer_handle,NULL,timer_function,parameter,
milliseconds_before_first_call,milliseconds_between_calls,
WT_EXECUTELONGFUNCTION /* the function takes a while, and may block */
);
}
Upvotes: 1