2

We have a (very) Legacy application written in VB6 (15 years old?).

The application contains a timer with 300ms interval. The Sub called when the timer ticks executes a batch of code that talks to some SQL servers, prints some labels and so on.

When everything is working OK, this Sub executes in 5ms to 10ms - i.e. before the next timer interval occurs - but it also wastes 290ms before the next tick.

We have a need to make this application a little faster, and one option is to change the interval to 1ms - before we do so, I would just like to confirm whether the timer will abort the interval (aka - completely ignore the tick) if the previous interval is still executing - or will it start building a stack of calls to the sub resulting in a hang after a while? (i am of course assuming all ticks get executed in the same thread as the gui – thus we’ll need to use DoEvents after every tick to ensure the UI doesn’t hang.)

I’ve tried looking into this, but finding reliable information on the old VB6 timers is proving tricky.

We do have this scheduled in to be re-written in .net using threading & background worker threads - this is just a short term fix that we're looking into.

3
  • 2
    Test it. use a timer with interval 1. Use a count variable. and in the timer event put count = count + 1: if count = 1 then: sleep(1000): end if check the count after 1.001 second. If it's 1001 then they stack. If it's 2 then they abort. my guess is stack. Use a button to enable the timer and a label to display the count.
    – Lefteris E
    Apr 26, 2013 at 10:57
  • +1 to Lefteris E's comment. Here is a link to the VB6 manual page on Timers, but I don't think it will answer your question.
    – MarkJ
    Apr 26, 2013 at 11:26
  • I've done some testing by outputting an incrementing var to a file with a sleep(2000) and DoEvents in every tick, it only appears to be incrementing the file every 2000 even though the timer is set to 1ms - DoEvents is keeping the UI responsive so it would appear the tick sub doesn’t stack, it "looks" like it just gets skipped - Would love to get a proper answer on this from someone in the know though - i was 12 years old in VB6 days so I don’t have a clue!
    – HeavenCore
    Apr 26, 2013 at 11:31

5 Answers 5

4

That's not how VB6 timers work, the Tick event can only fire when your program goes idle and stops executing code. The technical term is "pumps the message loop again". DoEvents pumps the message loop. It is a very dangerous function since it doesn't only dispatch timers` Tick events, it dispatches all events. Including the ones that lets the user close your window or start a feature again while it is still busy executing. Don't use DoEvents unless you like to live dangerously or thoroughly understand its consequences.

Your quest to make it 300 times faster is also doomed. For starters, you cannot get a 1 millisecond timer. The clock resolution on Windows isn't nearly high enough. By default it increments 64 times per second. The smallest interval you can get is therefore 16 milliseconds. Secondly, you just can't expect to make slow code arbitrarily faster, not in the least because Tick events don't stack up.

You can ask Windows to increase the clock resolution, it takes a call to timeBeginPeriod(). This is not something you ought to contemplate. If that would actually work, you are bound to get a visit from a pretty crossed dbase admin carrying a blunt instrument when you hit that server every millisecond.

1
  • We are aware that 1ms is unattainable :) The objective was to merely eliminate any idle time between ticks. Anyway, we did some testing and decided to leave it at 300ms and increase performance of the SQL instead (I found several table scans in the execution plans!) - Much easier and has proven to be far more effective than messing with the 15 year old binary :)
    – HeavenCore
    Apr 26, 2013 at 15:54
2

If the timer is a GUI component, (ie. not a thread pool timer), and fired by WM_TIMER 'messages', then the 'OnTimer' events cannot 'stack up'. WM_TIMER is not actually queued to the Windows message queue, it is synthesized when the main thread returns to the message queue AND the timer interval has expired.

1

When everything is working OK, this Sub executes in 5ms to 10ms - i.e. before the next timer interval occurs - but it also wastes 290ms before the next tick.

This is exactly what you have set it up to do if the time interval is 300ms. It is not wasting 290ms, it is waiting until 300ms has elapsed before firing the Tick event again.

If you want it to execute more often, then set the Time interval to 1ms, Stop the timer at the start of the Tick event and start it again when you have finished processing. That way there will only ever be 1ms idle time between operations.

0

If you put your timer interval faster than your execution time, this lock will probably allow you to execute your code as quickly as you can in VB6.

Private isRunning As Boolean

Private Sub Timer1_Tick()
    If Not isRunning Then
        isRunning = True
        'do stuff
        isRunning = False ' make sure this is set even in the event of an exception
    End If
End Sub

However, if you are inside this event handler as much as you want to be, or as fast as possible, close to 100% of the time, your application will become slow to respond to or unresponsive to UI events. If you put the DoEvents inside the do stuff you will give the UI a chance to process events, but UI events will halt execution inside do stuff. Imagine moving the window and halting execution... In that case, you probably want to spawn another thread to do the work outside of the UI thread, but good luck doing this in VB6 (I hear it's not impossible).

0

To maximize speed, with a looping set of instructions, remove the timer all together and have it a function called one at the end of the program entry point (Sub Main or Form_Load).

Within the function, Do a loop and use QueryPerformanceCounter to manage the repeat interval. This way you remove the overhead of the timer message system and can get around the minimal timer interval that exists with the timer. Add Doevents once at the the top of the Loop so the loop so other events can fire; and consumes idle time while waiting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.