Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Running Multiple Timers

I've created a stopwatch application based on API call 'timeGetTime' which can show 1/100 sec count.

The problem is that if I reproduce this timer (so that we have stopwatches A and B) and I launch both of them from a Form and run them simultaeously, the digital displays of timer A and B do not 'tick over' like they should. Only ONE timer at any given time shows the time counting, while the display of the other simply 'freezes'. Only when one stopwatch is stopped, does the other continue to 'tick over'.

Note that the stopwatch whose display isn't refreshing while the other is running, does continue to count the time correctly in the 'background' however, because when the display of the former resumes ticking over, it shows the correct elapsed time ie it resumes from the right place.

Any ideas as to how I can get the displays of both timers to show the time counting?

1 Solution
Have you tried adding "DoEvents" to each of the timer procedures.
DoctorNashAuthor Commented:
Yes the routines do contain 'Do Events'. Each timeGetTime section which loops until 1/100 s is counted, contains this command. I know this works because when a single timer is running, the application can indeed continue to do other things. Maybe in the case of multiple timers, Do Events is required in other places in the code? Would posting the code here help?
There may be several reasons why this is happening.

1) Force a refresh of the label (or whatever) when it increments.

2) Your threads may not be running simultaneously.
Try putting your code in one subroutine and update all timers in an array at once.
For Example, something like this:

Dim lTimeElapsed as Long
Dim lLastTime as Long
Dim myTimerArray() as Long
Dim iNumberofTimers as Integer
Do While (True)
 lTimeElapsed = timeGetTime - lLastTime
 lLastTime = lLastTime + lTimeElapsed
 For i = 0 to iNumberofTimers - 1
     myTimerArray(i) = myTimerArray(i) + lTimeElapsed
     labelTimer(i).Caption = myTimerArray(i)
 Next i
'I'm sure this could be optimized, just thinking off the top of my head.  :-)

Posting your code might help.
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
I may be suffering from ego-inflation, but I think my answer was best.
More help may be offered if the code is posted by DoctorNash.

However, one more suggestion is to simply place all the timer displays on one form, resize to fit and maybe add a little "X" box to close individual timers without closing the entire form.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now