Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. problem with basic timer in vb.net [modified]

problem with basic timer in vb.net [modified]

Scheduled Pinned Locked Moved Visual Basic
helpcsharpvisual-studioquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chun2
    wrote on last edited by
    #1

    I am using a timer with 1000ms interval to count secs. I need to perform certain functions after regular intervals. The problem is the timers are not accurate. Every 30 mins I lose appx 25 secs i.e., when timer starts count=1 and after 30 mins the count is appx 1775 instead of 1800. I actually took a stop watch and timed it. This way I am losing a lot of time by the end of the day. I have tried using System.Timers and system.windows.forms.timer but both have the same problem. I am using VS 2005 express edition If I use timer with interval 100ms .. it is even worse. Am I missing something? Please help.

    modified on Sunday, April 6, 2008 11:09 AM

    C 1 Reply Last reply
    0
    • C Chun2

      I am using a timer with 1000ms interval to count secs. I need to perform certain functions after regular intervals. The problem is the timers are not accurate. Every 30 mins I lose appx 25 secs i.e., when timer starts count=1 and after 30 mins the count is appx 1775 instead of 1800. I actually took a stop watch and timed it. This way I am losing a lot of time by the end of the day. I have tried using System.Timers and system.windows.forms.timer but both have the same problem. I am using VS 2005 express edition If I use timer with interval 100ms .. it is even worse. Am I missing something? Please help.

      modified on Sunday, April 6, 2008 11:09 AM

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      Well this timers were never indented to be precise (there are a few "better" timers in the DirectX-SDK). When I get your question correct all you need to do is still use your timer (let's say with an 500ms interval) and just check the timespan since last run: Define a private _lastCallTime as DateTime = DateTime.MinValue in your form-class and in your Tick-Handler: if DateTime.Now.Substract(_lastCallTime) >= TimeSpan.FromMinutes(30) then _lastCallTime = DateTime.Now // TODO: call your function here end if

      C 1 Reply Last reply
      0
      • C CKnig

        Well this timers were never indented to be precise (there are a few "better" timers in the DirectX-SDK). When I get your question correct all you need to do is still use your timer (let's say with an 500ms interval) and just check the timespan since last run: Define a private _lastCallTime as DateTime = DateTime.MinValue in your form-class and in your Tick-Handler: if DateTime.Now.Substract(_lastCallTime) >= TimeSpan.FromMinutes(30) then _lastCallTime = DateTime.Now // TODO: call your function here end if

        C Offline
        C Offline
        Chun2
        wrote on last edited by
        #3

        Thank you for your response. This will be temporary fix for one part of my program.In my application a lot of events depend on the One second timer. I also have to display the counter counting up, trigger various events when it reaches certain values and then reset. These values depend on lot of parameters and keep changing. I really need a good timer. I have seen many applications where they use software timers to trigger events. I dont know how they do it, may be they use one of the "better" timers like you have suggested. (1) Could you please suggest some links where i can find examples for using one of these timers in vb.net application? (2) I searched online for directX SDK download which would worrk with vb.net on vista OS. I found the following link. Microsoft states that this file works with c/c++ and C# but doesnot mention vb.net. The other links I found for vb.net donot work with vista :( Any suggestions? http://www.microsoft.com/downloads/details.aspx?FamilyId=4B78A58A-E672-4B83-A28E-72B5E93BD60A&displaylang=en again... thanks a lot for your input!!

        D 1 Reply Last reply
        0
        • C Chun2

          Thank you for your response. This will be temporary fix for one part of my program.In my application a lot of events depend on the One second timer. I also have to display the counter counting up, trigger various events when it reaches certain values and then reset. These values depend on lot of parameters and keep changing. I really need a good timer. I have seen many applications where they use software timers to trigger events. I dont know how they do it, may be they use one of the "better" timers like you have suggested. (1) Could you please suggest some links where i can find examples for using one of these timers in vb.net application? (2) I searched online for directX SDK download which would worrk with vb.net on vista OS. I found the following link. Microsoft states that this file works with c/c++ and C# but doesnot mention vb.net. The other links I found for vb.net donot work with vista :( Any suggestions? http://www.microsoft.com/downloads/details.aspx?FamilyId=4B78A58A-E672-4B83-A28E-72B5E93BD60A&displaylang=en again... thanks a lot for your input!!

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Out of all the Timers that come in the .NET Framework, the System.Windows.Forms.Timer class is about the worst when it comes to raising it's event on time. System.Timers.Timer is better, but can still be late. The System.Threading.Timer is the most accurate, but is also the hardest to use. In any case, counting ticks of a timer is about the worst way you can implement this. You should be using a rather fast firing timer (accurate or not) and comparing the current time to the scheduled time of a process you need to launch, even if it's every one second. If you're code is doing other things and the UI thread is blocked for longer than your Timer's Interval is set for, using anything but the Threading.Timer will result in your tick count not being accurate because the events cannot be processed until the UI thread is unblocked. Another possible issue is something else running on the machine could be screwing with the O/S-based timers that the .NET Timer classes rely on. Be it bad hardware or an O/S problem.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups