berdar
berdar

Reputation: 11

How can i implement time driven tasks in C on win32 platforms?

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

Answers (3)

Mark
Mark

Reputation: 588

Take a look at the TimerQueue related functions:

http://msdn.microsoft.com/en-us/library/ms682483(v=vs.85).aspx

Upvotes: 0

Anthony Williams
Anthony Williams

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

rerun
rerun

Reputation: 25495

Using c you can use the win32 SetTimer Function

Upvotes: 1

Related Questions