tags:

views:

603

answers:

9

Hello All,

How do I create a timer in C and after the time expires ,I should be able to call a callback function . The platform is windows.

Can someone guide me

Regards, Mithun

+1  A: 

alarm does this (on POSIX platforms).

Will
A: 

Timers are not part of the C language.

You should look up the documentation for whichever programming environment it is that you are using to figure out how to do timers. As you have not indicated what environment, it isn't possible to answer your question.

Matthias Wandel
The platform is windows .
mithunmo
+5  A: 

Take a look at SetTimer function.

UINT_PTR timerid = SetTimer(NULL, 0, milliseconds, &callback);
avakar
...If it's a windowed Win32 C application.
Kieveli
Yes, it must have a message loop. It needn't have any windows, though.
avakar
A: 

If you are willing to tackle WIN32 Thread Pools, you can use thread pool timers.

A thread pool is a collection of worker threads that efficiently execute asynchronous callbacks on behalf of the application. The thread pool is primarily used to reduce the number of application threads and provide management of the worker threads. Applications can queue work items, associate work with waitable handles, automatically queue based on a timer, and bind with I/O.

A part of this API, see

CreateTimerQueueTimer Function

Creates a timer-queue timer. This timer expires at the specified due time, then after every specified period. When the timer expires, the callback function is called.

gimel
+1  A: 

If you don't need to be doing anything else in your application, you can use the sleep() function. It will stop execution from continuing, and then resume executing code after the specified duration has expired (ish).

In Windows, we used to use a multimedia timer function if you wanted to sleep for less than one-second intervals. Some other methods are platform-dependent.

I've also heard of people using 'select()' as a method of sleeping for milli / micro seconds.

If you need other things to be happening, you're looking to using the sleep / select within a second thread.

Here's a Google Library for doing high resolution timing as well. It might help.

Kieveli
+1 because I liked the google reference.
SoMoS
A: 

Take a look at the MAME emulator implementation.

BenB
A: 

One way you can do it is to call SetTimer and then handle the WM_TIMER event in your WndProc. For example, this would setup a timer that gets called every 45 seconds and displays a message box when it's called:

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
     case WM_CREATE:
      SetTimer(hwnd, 1, 45000, NULL);
      break;

     case WM_TIMER:
      MessageBox(hwnd, L"Timer", L"Timer fired!", MB_OK);
      break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

The first parameter to SetTimer is the window that you want to receive the WM_TIMER message -- you probably want this to be your window so you can just pass in the hwnd that windows passed you.

The second parameter is a number that you can use to uniquely identify the timer. You could have more than one timer running at a time and would need a way to tell which one fired.

The third parameter is how long you want the timer to wait before it fires. It is in milliseconds, so you have to multiple by a thousand if you want seconds.

The forth parameter is NULL if you want to handle timers by looking at WM_TIMER. Otherwise, you can pass a pointer to a callback function that will be called instead.

Remember, the timer will keep firing every X milliseconds until you kill it. You can kill it by calling KillTimer and passing in the same number you passed to the second parameter when you called SetTimer.

Also, when Windows sends you the WM_TIMER message, the wParam will contain the timer identifier that you passed in the second parameter when you called SetTimer.

Slapout
A: 

Probably a bit off target, and it's C++ not straight C but here's my implementation of a timer queue for Windows complete with tests and a commentary on TDD. Note that the first part in the series explains why I decided to roll my own rather than use the alternatives.

http://www.lenholgate.com/archives/000306.html

Len Holgate
A: 

From Timers and default actions:

/* ** TIMEGETC.C - waits for a given number of seconds for the user to press ** a key.     Returns the key pressed, or EOF if time expires ** ** by Bob Jarvis */

#include <stdio.h>
#include <time.h> 
#include <conio.h>

int timed_getch(int n_seconds) 
{
   time_t start, now;

   start = time(NULL); now = start;

   while(difftime(now, start) < (double)n_seconds && !kbhit()) { now = time(NULL); }

   if(kbhit()) 
       return getch(); 
   else 
       return EOF;
}

void main(void)
{
   int c;

   printf("Starting a 5 second delay...\n");

   c = timed_getch(5);

   if(c == EOF)
       printf("Timer expired\n");
   else 
       printf("Key was pressed, c = '%c'\n", c);
}

But I would consider using Window's ::SetTimer(), as mentioned above...

Eugene