Windows Form application Timer Tick

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Henrik Söderber
    New Member
    • Jan 2011
    • 4

    Windows Form application Timer Tick

    Hi!

    I use C# 4.0 to build a windows form application.
    I have a Windows Forms Timer that should tick every second. In every tick I do some work (get GPS position, update a map, save to a local DB etc). It takes about 0.09 sec to execute. The save to the local DB is done async to release the GUI thread faster but the other code is executed on the GUI thread (need to for updating the GUI)

    Some time the tick is not fired constantly, it can be several seconds between (I guess the GUI thread is busy in some way).

    How can I rewrite so that there always will be a tick on time, is it possible? The program runs on machine with a single processor. Most of the time the CPU usage is not very high.

    Would there be any meaning to switch to a System.Threadin g Timer to get more accuracy? I do need to be on the GUI thread most of the time during the Timer execution.

    Regards
    Henrik
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I'd recommend looking into threading or a background worker for this. When your timer ticks, create a thread to handle your update. You can have attach an event to when it's finished so that your GUI can do anything else it needs to when that's finished.

    I notice you said you are doing this asynchronously so perhaps you already know about this. I figured I'd mention it anyway :)

    What I'm curious about is why your timer doesn't tick on time. You've got it to tick every second and that's plenty of time for you to get your calculations done. So if it's not ticking on time, there's something else going on here. What would be preventing the timing, especially since your work is getting done asynchronously?

    Is the GUI update taking longer than your 1 second tick window?

    Comment

    • Henrik Söderber
      New Member
      • Jan 2011
      • 4

      #3
      The GUI update should be much faster than 1 second. but since there are other software involved (3:part) updating the GUI, its hard to know how much time and power it takes.

      But something is taking time, I have tried to do as much as possible in other threads (async).

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Have you considered having separate timers for your GUI update and your logic updates?

        Comment

        • Henrik Söderber
          New Member
          • Jan 2011
          • 4

          #5
          Yes, I have considered many different solutions. I do think that the GUI update must be tight connected to the other updates for it to be real-time feeling for the user...

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            I don't know that's not always true. Where I'm coming from is a gaming standpoint. Often, a game's update loop happens more often than the display loop. Really, the human eye/brain can only comprehend information so quickly so it makes a bit of sense. The display method doesn't generate/process any new information, it only displays graphically the current state of the information.

            I'm just throwing this out there for you to consider. If you can't do this in your program, you can't. At some point though, if your updates/display are taking longer than your time slice you'll either have to expand your time slice or do less work in each time slice. You can accomplish the latter by either reducing the complexity of your code, or performing some tasks in alternating time slices.

            Comment

            Working...