CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2011
    Posts
    0

    Issue with timer event handler - Vc++

    Hello.
    I started a new windows form in visual studio 2010 using C++ language.
    There is only one timer configured to generate an event each 1ms (1milisecond)

    Inside the timer event handler, I just increment a variable named Counter (who is used only in this event) and I write the current value of this variable in a textbox, so that I can see its current value.

    Considering that the timer event occurs each 1ms, for each 1 second, the variable Counter should increment 1000 times, but the Counter variable takes around 15 seconds to increment 1000 times. After 15 seconds the value shown in textbox is 1000.

    I set the timer event to 1ms, but seems that the event is occuring only each 15ms, because the variable Counter took 15 times (15 seconds) more than in theory to reach the value of 1000 (1 second = 1000*1ms).

    Someone have an ideia on how to solve this problem?



    Uploaded with ImageShack.us

    I need to generate an event each 1ms, where I will call another function.
    How cold I generate an event each 1ms interval? Or less than this if possible.

    A person of anther forum told me to create a Thread to do this job but I don't know how to do that.

    Im using windows 7 profesional 64bits, I don't know if 64bits OS have any relationship with this issue. I think the PC hardware is enough to generate the event. Core 2 duo 2GHz and 3GB RAM.

  2. #2
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Issue with timer event handler - Vc++

    Since your timer consumes most of the processing time (ie. every 1ms), you have effectively suspended the UI thread preventing it from updating the form in real time. With heavy cpu usage, you'll need to use multiple threads to make sure the UI is updated in real time.
    Gort...Klaatu, Barada Nikto!

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Issue with timer event handler - Vc++

    http://www.codeproject.com/KB/system/timers_intro.aspx

    "It is important to understand that the accuracy of timers is limited. Windows is not a real-time operating system (except Windows CE) and it is not reasonable to expect timers to handle very small time intervals (10 ms, for instance)."


    Windows timers aren't nearly that accurate.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Issue with timer event handler - Vc++

    Quote Originally Posted by seccoxiru2 View Post
    I set the timer event to 1ms, but seems that the event is occuring only each 15ms, because the variable Counter took 15 times (15 seconds) more than in theory to reach the value of 1000 (1 second = 1000*1ms).
    Close. AFAIK this sort of timer has a resolution of 15.625 ms (64 Hz). However, in practice you can't even completely rely on your app catching each and every one of its ticks if you set its interval to something as relaxed as 1000 ms.

    Here's a thread with a brief breakdown on timers and multithreading in the .NET framework (without any claim of completeness) I just happened to write last night: http://www.codeguru.com/forum/showthread.php?t=518393. That thread, BTW, is in the correct forum section to post topics about C++/CLI like yours in.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

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