|
-
May 4th, 2013, 10:18 PM
#1
Thread Starter
Addicted Member
[RESOLVED] a tale of two timers
Hi folks.
I have a form with a label. I want the form to display a message every 5 seconds, and the message to display for 700ms before closing. So I used timer1 with an interval of 5000ms, and timer2 with an interval of 700ms. But the message seems to be displaying every 5 seconds(what I want), but is displayed for 5 seconds(what I don't want). I want the message to display only for 700ms. Where is the problem in the code? Thanks in advance.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer2.Start()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If Me.Visible = True Then
Me.Visible = False
Else
Me.Visible = True
Me.Label1.Text = "Test Message"
End If
Timer2.Stop()
End Sub
End Class
-
May 5th, 2013, 01:02 AM
#2
Re: a tale of two timers
Try using a single timer running often enough to do both jobs of turning on and turning off the message.
Retain the last time turned on so you can manage the on/off.
-
May 5th, 2013, 01:56 AM
#3
Thread Starter
Addicted Member
Re: a tale of two timers
Thanks for your reply. I will give your suggestion a go, but before that, could you explain why my program displays the message for 5 seconds, instead of 7/10 of a second? I am looking at my code, and I just can't figure out this strange behaviour. Thank you.
-
May 5th, 2013, 02:27 AM
#4
Re: a tale of two timers
It is because you have explicitly tied the speed to Timer1.
Each time Timer1 fires, you start Timer2. When Timer2 fires, you do the work and disable Timer2 - and then have to wait until Timer1 starts it again.
The simplest solution is to make two changes (add a line, and move one):
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer2.Start()
Timer1.Stop()
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If Me.Visible = True Then
Me.Visible = False
Timer2.Stop()
Else
Me.Visible = True
Me.Label1.Text = "Test Message"
End If
End Sub
End Class
szlamany's suggestion is more elegant and self-contained, but takes a bit more code... this is one way it could be done (with just Timer1):
Code:
Public Class Form1
Private TimerStartedAt as Date
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TimerStartedAt = Now()
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Select Case (Now - TimerStartedAt).TotalMilliseconds
Case 5000 To 5700
If Not Me.Visible Then
Me.Visible = True
Me.Label1.Text = "Test Message"
End If
Case > 5700
Me.Visible = False
Timer1.Stop()
End Select
End Sub
End Class
Last edited by si_the_geek; May 5th, 2013 at 02:33 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|