Thread: How to implement software timers for C GNU code ?

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    76

    How to implement software timers for C GNU code ?

    Hi,

    I searched and tried to compile some snippets and example codes from online but faced some problems.

    Last one, this:

    Timer framework in C - Code Review Stack Exchange

    But got this error:

    sys/queue.h: No such file or directory
    And I think the code is done for Linux.

    I have windows 10, working on codeblocks GNU C programming.

    Any suggestions ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,620
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    Thank you so much for the links ! I tried the snippet and it worked, I got the timeout action.

    But I want a simpler one, so I tried the "SetTimer" in this link:

    SetTimer function (winuser.h) - Win32 apps | Microsoft Docs

    I don't know what it's actually, a struct or a function, so I tried to initialize it, I read some of the information but didn't know how to work with it. I tried this code as a start.

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdbool.h>
    #include <inttypes.h>
    #include "../soft_timers/soft_timers.h"
    #include "../thread_a/thread_a.h"
    
    
    void action1(void){
        printf("TimerRoutine action\n");
    }
    
    
    UINT_PTR SetTimer(
      HWND      hWnd,
      UINT_PTR  nIDEvent,
      UINT      uElapse,
      TIMERPROC lpTimerFunc
    );
    
    
    
    
    hWnd = NULL;
    nIDEvent = NULL;
    uElapse = 1000;
    lpTimerFunc = action1;
    
    
    
    
    int main()
    {
    
    
    
    
    
    
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,620
    > I don't know what it's actually, a struct or a function,
    It's quite obviously a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    OK, got this code to work.

    Code:
    /*	Title:	library manager
    	Date:	Mon, 11 Jun 2021
    */
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdbool.h>
    #include <inttypes.h>
    
    
    typedef void (*f_ptr)();
    
    
    void CALLBACK action1(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime){
        printf("TimerRoutine action\n");
    }
    
    
    f_ptr fptr;
    int i = 0;
    
    
    int main()
    {
        MSG msg;
    
    
        fptr = action1;
    
    
        SetTimer(NULL,0,1000,fptr);
    
    
    
    
        while(1){
            if(GetMessage(&msg, NULL, 0, 0)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            i++;
            if(i > 4){break;}
        }
    
    
        return 0;
    }

    A note here is that:

    Code:
    TranslateMessage(&msg);
    I don't know what it does but I commented it and the code works the same way, what it does exactly ?


    ===========================================
    Now, is there a way to run a timer in the back ground and I can take it's time counts any time ?

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    I'm working on this one:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <stdbool.h>
    #include <inttypes.h>
    
    
    
    
    #define TIMER1 0
    #define TIMER2 1
    
    
    typedef void (*f_ptr)();
    
    
    void CALLBACK action1(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime){
        printf("action1\n");
    }
    
    
    void CALLBACK action2(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime){
        printf("action2\n");
    }
    
    
    f_ptr fptr1;
    f_ptr fptr2;
    
    
    int i = 0;
    
    
    int main()
    {
        MSG msg1, msg2;
    
    
        fptr1 = action1;
        fptr2 = action2;
    
    
        SetTimer(NULL,TIMER1,1000,(TIMERPROC)fptr1);
        SetTimer(NULL,TIMER2,500,(TIMERPROC)fptr2);
    
    
    
    
        while(i < 3){
            if(GetMessage(&msg1, NULL, 0, 0)) { DispatchMessage(&msg1); }
            i++;
        }
    
    
        while(1){
            if(GetMessage(&msg2, NULL, 0, 0)) { DispatchMessage(&msg2); }
        }
    
    
    
    
    
    
        return 0;
    }

    Why the first while loop doesn't break or stop, I put a condition for its termination.

    Loop 2 should run forever, but I thought loop 1 should break.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,620
    > I don't know what it does but I commented it and the code works the same way, what it does exactly ?
    Read the M$ manual page for it.

    > Why the first while loop doesn't break or stop, I put a condition for its termination.
    Go back to the original message dispatch loop you had.

    There is only one message dispatch loop in a windows program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jan 2016
    Posts
    76
    OK, I want to get the timer counting mode not time elapse mode as with the timeout.

    I want the timer to run in the background and start running, counting from 0 to overflow and start again, not stopping.

    And then, any time during program run, I read the timer count value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help us implement code (C programming) which calls controlling tasks ...
    By concept_one in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 11-10-2015, 12:04 PM
  2. Implement a loop into my code?
    By CtrlAltElite in forum C++ Programming
    Replies: 6
    Last Post: 11-26-2011, 10:10 AM
  3. trying to implement this code in c
    By centenial in forum C Programming
    Replies: 6
    Last Post: 02-16-2010, 06:56 PM
  4. Timers and software interrupts
    By Afro1986 in forum C Programming
    Replies: 6
    Last Post: 01-21-2009, 06:21 AM
  5. Replies: 9
    Last Post: 11-12-2007, 03:29 PM

Tags for this Thread