Thread: win32 - timeSetEvent(): what is wrong with these function?

  1. #16
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Seems like you only get one timeSetEvent with your setup. Perhaps the call back function could set an array of events, each of which in turn would call back TimerProc() .

  2. #17
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    what you advice?
    heres my entire Timer class:
    Code:
    class Timer
    {
    
    private:
        UINT_PTR timerid=0;
        UINT m_uResolution=0;
        unsigned int intInterval=0;
    
        static void CALLBACK _TimerProc(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD dw1, DWORD dw2)
        {
            Timer* obj=reinterpret_cast<Timer*>(dwUser);
            obj->timerprocedure();
        }
    
    public:
    
        std::function<void()> timerprocedure=NULL;
        Timer(std::function<void()> tmrprocedure=NULL)
        {
            timerprocedure=tmrprocedure;
        }
    
        void Stop()
        {
            if(timerid!=0)
            {
                timeKillEvent(timerid);
                timeEndPeriod (m_uResolution);
            }
        }
    
        property <unsigned int> Interval
        {
            Get(unsigned int)
            {
                return intInterval;
            },
            Set(unsigned int uintInterval)
            {
                intInterval = uintInterval;
            }
        };
    
        void Start()
        {
            Stop();
            TIMECAPS tc;
            timeGetDevCaps(&tc, sizeof(TIMECAPS));
            m_uResolution = min(max(tc.wPeriodMin, 0), tc.wPeriodMax);
            timeBeginPeriod(m_uResolution);
            timerid = timeSetEvent(intInterval, m_uResolution, Timer::_TimerProc, reinterpret_cast<DWORD_PTR>(this),TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
    
            if (timerid==0)
                DebugText("error\t" + to_string(GetLastError()));
        }
    
        ~Timer()
        {
            Stop();
        }
    };
    timerprocedure it's a lambda. i don't know if i can use it on array way.
    Last edited by joaquim; 04-30-2016 at 04:47 AM.

  3. #18
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I meant an array or set of events. Use timeSetEvent to call a helper function. That function in turn will set up to 7 events, each which in turn can call a timer function,

  4. #19
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    i'm sorry. but don't make sence to me
    thinking on that: what is for the 'this'?

  5. #20
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    It would help if you could explain how the 7 instances of the timer class are being used.

  6. #21
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I'm able to create two timers with Visual Studio / Win XP, so there may be an issue with your setup. This example is based on old code.

    Code:
    #include <windows.h>
    #include <stdio.h>
    
    #pragma comment(lib, "winmm.lib")       /* include winmm.lib */
    
    /*----------------------------------------------------------------------*/
    /*      data                                                            */
    /*----------------------------------------------------------------------*/
    static TIMECAPS sTimeCaps;              /* min and max timer values */
    static MMRESULT mmTimerId0;             /* timer id */
    static MMRESULT mmTimerId1;             /* timer id */
    static volatile DWORD dwTicks0;         /* tick counter */
    static volatile DWORD dwTicks1;         /* tick counter */
    
    /*----------------------------------------------------------------------*/
    /*      code                                                            */
    /*----------------------------------------------------------------------*/
    void CALLBACK TimerFunction0(UINT, UINT, DWORD, DWORD, DWORD);
    void CALLBACK TimerFunction1(UINT, UINT, DWORD, DWORD, DWORD);
    
    /*----------------------------------------------------------------------*/
    /*      main                                                            */
    /*----------------------------------------------------------------------*/
    int main(int argc, char **argv)
    {
    /*                                      ** get min and max period */
        timeGetDevCaps(&sTimeCaps, sizeof(sTimeCaps));
        if(sTimeCaps.wPeriodMin != 1){
            printf("1 ms resolution not allowed\n");
            goto exit1;}
    
        timeBeginPeriod(1);                 /* set period to 1ms */
        Sleep(128);                         /* wait for it to stabilize */
    
    /*                                      ** create two 4ms timers */
        mmTimerId0 = timeSetEvent(4, 0, TimerFunction0, 0, TIME_PERIODIC);
        if(mmTimerId0 == (MMRESULT)0){
            printf("Failed to generate multimedia timer 0.\n");
            goto exit0;}
        Sleep(2);
        mmTimerId1 = timeSetEvent(4, 0, TimerFunction1, 0, TIME_PERIODIC);
        if(mmTimerId1 == (MMRESULT)0){
            printf("Failed to generate multimedia timer 1.\n");
            goto exit0;}
        Sleep(2048);                        /* wait 2.048 seconds */
    
    exit0:
        if(mmTimerId0)
            timeKillEvent(mmTimerId0);
        if(mmTimerId1)
            timeKillEvent(mmTimerId1);
        timeEndPeriod(1);                   /* restore resolution to default */
    
        printf("dwTicks0 %d\n", dwTicks0);
        printf("dwTicks1 %d\n", dwTicks1);
    
    exit1:
        return(0);
    }
    
    /*----------------------------------------------------------------------*/
    /*      TimerFunction       increments dwTicks                          */
    /*----------------------------------------------------------------------*/
    void CALLBACK TimerFunction0(UINT uTimerID, UINT uMsg,
                            DWORD dwUser, DWORD dw1, DWORD dw2)
    {
        dwTicks0 += 1;                           /* increment ticks */
    }
    
    /*----------------------------------------------------------------------*/
    /*      TimerFunction       increments dwTicks                          */
    /*----------------------------------------------------------------------*/
    void CALLBACK TimerFunction1(UINT uTimerID, UINT uMsg,
                            DWORD dwUser, DWORD dw1, DWORD dw2)
    {
        dwTicks1 += 1;                           /* increment ticks */
    }

  7. #22
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I compiled and tested the same code from post #21 using Visual Studio Express 2015 and Windows 7, and it runs OK, so having multiple setTimeEvent() isn't an issue for Win XP or Win 7.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timeSetEvent
    By Mole42 in forum Windows Programming
    Replies: 3
    Last Post: 09-15-2010, 10:55 AM
  2. win32 timer function
    By Anuradh_a in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2008, 11:51 AM
  3. timeSetEvent problem
    By likeregularchic in forum Windows Programming
    Replies: 6
    Last Post: 09-10-2005, 07:36 PM
  4. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 08:55 PM
  5. compiling mingw to enable timeSetEvent
    By underthesun in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2005, 06:00 PM