Timers

celtic

New member
Joined
May 30, 2010
Messages
4
Programming Experience
1-3
Hi all.

I was wondering if someone could enlighten me regarding the function of timers on a windows form.

Assuming we have a timer's interval set for let's say 5 seconds and some code in the click event that gets executed which may take some microseconds.
Does the the program just wait until the next click event of the timer before returning control to the calling routine?
If that was so, would we would need to include a DoEvents so that other windows processes can execute while our program waits?
But if we included a DoEvents, would our calling routine continue to execute without waiting for the timer to 'return'?

I'm asking the questions to improve my understanding of timers in VB.

What I would like then to do is use 2 timers in a windows form application.
Something like the following:
timer1 would execute the code in its click event, and when it's interval is over, timer2 would execute the code in it's click event, at which time timer1 would begin again.
This would continue until another event like a button click which would disable the timers.

What would be the best way to accomplish this?

Thank you in advance

--Celtic
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,379
Location
Sydney, Australia
Programming Experience
10+
A System.Windows.Forms.Timer raises its Tick event (not Click) repeatedly as specified by the Interval property and it does so on the UI thread. That's it, that's all. If your Tick event handler takes longer to execute than the Interval property value then you will get multiple events queued and will eventually run into problems. As with any method, you should never have code in a Tick event handler that takes a long time and runs synchronously. Anything time consuming should be explicitly executed on another thread or executed asynchronously.

Rather than describing what you want to do with Timers, how about you just describe what you're trying to achieve and then we can advise whether using Timers is the most appropriate approach and, if so, exactly how it should be done? What you describe is a bit vague but certainly doesn't sound like something you should be doing.
 

celtic

New member
Joined
May 30, 2010
Messages
4
Programming Experience
1-3
Thank you for your reply jmcilhinney.

Yes, sorry. I meant the tick event.

I see. That makes sense. Now that I know that, I know how to make it work with timers.
However, in the interests of learning more: what I wanted to do was to put together a simple flashcard program (completed, but using timers)
So for example, the 'front' of the card (right now just a label), would change what it shows every 'x' seconds (user settable) as it runs through the deck of 'cards'.
The 'back' of the card would auto-delay what it shows (user settable), and then the front of the next 'card' would show 'x' seconds after the back of the 'card' shows.

This would continue until the user pauses or stops the process.
I assumed that a couple of timers would suffice.
(what's in the timer tick event of both timers is a call to a synchronous process in a sub-routine).

If not timers, how could I make this work?
(I haven't looked at anything regarding multiple threads).

--Celtic
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,379
Location
Sydney, Australia
Programming Experience
10+
You would only need one Timer. You can just change the Interval on each Tick, e.g.
VB.NET:
Private watch As Stopwatch

Private interval1 As Integer = 1000
Private interval2 As Integer = 3000

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    watch = Stopwatch.StartNew()

    Timer1.Interval = interval1
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Timer1.Interval = If(Timer1.Interval = interval1, interval2, interval1)

    Console.WriteLine(watch.Elapsed)

    watch = Stopwatch.StartNew()
End Sub
You can just use an If statement to determine what to in the Tick event handler based on the current Interval value. If the two intervals may be the same, you could use a Boolean flag that you simply set negate each Tick so you know for sure which of the two phases you're currently in.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,379
Location
Sydney, Australia
Programming Experience
10+
If you do have lengthy tasks to perform on a Tick then you should certainly do so asynchronously in some manner. One thing to consider is the fact that a System.Timers.Timer works very similarly to a System.Windows.Forms.Timer but raises its Elapsed event on a secondary thread by default. It may be preferable to do that and then only marshal back to the UI thread for those things that must be done there, i.e. code that affects controls.
 
Top Bottom