How to implement a timer in C on windows

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

Can anyone guide me

Regards, Mithun

+6
c winapi timer
source share
9 answers

Take a look at the SetTimer function .

UINT_PTR timerid = SetTimer(NULL, 0, milliseconds, &callback); 
+7
source share

alarm does this (on POSIX).

+1
source share

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 :: SetTimer (), as mentioned above ...

+1
source share

Timers are not part of C.

You should find the documentation for any programming environment that you use to figure out how to make timers. Since you did not indicate which environment, it is impossible to answer your question.

0
source share

If you want to do WIN32 thread pools , you can use thread pool timers.

A thread pool is a collection of workflows that efficiently perform asynchronous callbacks on behalf of an application. The thread pool is mainly used to reduce the number of application threads and provide workflow management. Applications can queue work items, associate work with returned descriptors, automatically timer-based queues, and communicate with I / O.

Part of this API, see

CreateTimerQueueTimer Function

Creates a timer timer. This timer expires at the specified time, and then after each specified period. When the timer expires, the callback function is called.

0
source share

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

On Windows, we used the multimedia timer function if you wanted to sleep in less than one second. Some other methods are platform dependent.

I also heard that people use "select ()" as a method of sleep per milli / microseconds.

If you need other things, you want to use sleep / select in the second thread.

Here's the Google Library for high-resolution timing . This can help.

0
source share
0
source share

One way to do this is to call SetTimer and then handle the WM_TIMER event in WndProc. For example, this sets a timer that is called every 45 seconds and displays a message box when it is 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 into which you want to receive the WM_TIMER message. You probably want this to be your window, so you can just walk into the hwnd that passed you by.

The second parameter is a number that you can use to uniquely identify the timer. You may have more than one timer running at the same time, and it will need a way to tell which one has been started.

The third parameter is how long you want the timer to wait before it starts. This is in milliseconds, so you need a few thousand if you want seconds.

The fourth 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 that the timer will continue to fire every X milliseconds until you kill it. You can kill it by calling KillTimer and passing the same number that you passed to the second parameter when calling SetTimer.

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

0
source share

Probably a bit from the target, and this C ++ is not direct C, but here is my implementation of the timer queue for Windows complete with tests and commentary on TDD. Note that the first part in the series explains why I decided to use my own rather than using alternatives.

http://www.lenholgate.com/blog/2004/05/practical-testing.html

0
source share

All Articles