CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    heres my timer class:
    Code:
    #define  min(x, y)   __min((x), (y))
    #define  max(x,y)   __max((x), (y))
    class Timer
    {
    
    private:
        UINT_PTR timerid=0;
        UINT m_uResolution=0;
        unsigned int intInterval=0;
    
        static void CALLBACK _TimerProc(UINT wTimerID, UINT msg, DWORD 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);
            }
        }
    
        unsigned int GetInterval()
        {
            return intInterval;
        }
    
        void SetInterval(unsigned int uintInterval)
        {
            intInterval = uintInterval;
        }
    
        property <unsigned int> Interval{GetProperty(Timer::GetInterval),SetProperty(Timer::SetInterval)};
    
        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>(this),TIME_PERIODIC);
    
            if (timerid==0)
                DebugText("error\t" + to_string(GetLastError()));
        }
    
        ~Timer()
        {
            Stop();
        }
    };
    or it's the 'this' or i don't know.
    after 6 or 7 Timer class instances the timeSetEvent() give me NULL and GetLastError():
    - 5: acess denied;
    i don't have sure about the error. but maybe it's the instance pointer. can anyone explain better to me?
    Last edited by Cambalinho; May 4th, 2016 at 05:02 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    Not sure, but when I look at the timeSetEvent in msdn, I see the following warning.

    Note This function is obsolete. New applications should use CreateTimerQueueTimer to create a timer-queue timer.

    I'm wondering if it's worth bothering debugging the obsolete approach?

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    Quote Originally Posted by Cambalinho View Post
    after 6 or 7 Timer class instances the timeSetEvent() give me NULL and GetLastError():
    - 6: acess denied;
    "Access denied" code is 5. And 6 is "The handle is invalid."
    Best regards,
    Igor

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    thanks Igor. my mistake on number. sorry.

    have something to do, the error, that the 6 timers instances use 200 ms(or less)? maybe i use several resources?

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    In a situation like this I would put aside all the fancy C++ husk, entirely, and write a brand new simplistic WinAPI app that would let me see if the situation with multiple timers is reproducible (of course, in accordance with my current understanding of root cause ).
    Best regards,
    Igor

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    these make me think about my multithread code. i did an array for resolve the problem, maybe i must do the same here with timerid
    Last edited by Cambalinho; May 5th, 2016 at 05:08 AM.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    Quote Originally Posted by Cambalinho View Post
    these make me think about my multithread code. i did an array for resolve the problem, maybe i must do the same here with timerid
    You did not mention multithreading inn your OP. Perhaps there is the main reason of your problem?
    How do you use multithreading and your class?
    Victor Nijegorodov

  8. #8
    Join Date
    Apr 2009
    Posts
    1,355

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    the problem is another that i didn't notice: we only can have 16 Multimedia Timers.... i was using, without notice, 16 or more. so that's natural that some of them fail. i'm resolving in a diferent way: if the interval is less than 10ms, then use timeSetEvent(), else use SetTimer(). now works. i will do another thread. Moderator: i'm so sorry, but i can't enter on these forum, again. i'm using https://eu3.proxysite.com for enter please tell me something.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: My Timer class: why, after 6 or 7 instances, the timeSetEvent() is NULL?

    Quote Originally Posted by Cambalinho View Post
    the problem is another that i didn't notice: we only can have 16 Multimedia Timers.... i was using, without notice, 16 or more.
    Well, but you told us about "6 or 7 instances", not about "16 or more" ...


    Quote Originally Posted by Cambalinho View Post
    ... Moderator: i'm so sorry, but i can't enter on these forum, again. i'm using https://eu3.proxysite.com for enter please tell me something.
    You should report this problem in the Feedback forum: only Admin could help you, not a moderator.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured