I have been doing some coding for a system and need to use threading instead of normal timers in VB.NET.
It works fine but the problem lies in the blink timings, when the button is clicked then it blinks as expected, if in testing it is clicked more than once then the blinking time roughly multiplies by the original sleep thread time (750ms), this continues to happen for every click.
What can I do to make the blink not speed up? Below is the code!
Private _flash As Boolean = False
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click
_flash = True
Dim FlashThread As New Thread(New ThreadStart(AddressOf FlashLabel))
Private Sub FlashLabel()
Dim _color As Color = Color.Gray
If label1.ForeColor.Equals(_color) Then
label1.ForeColor = Color.Red
label1.ForeColor = Color.Gray
Every time the button is clicked, you are starting a new thread, so if you click the button twice, it will start two threads, both of which are toggling the colors at 750 millisecond intervals, so it appears as if there was one thread doing it twice as fast. A simple way around this is to simply skip starting the new thread if the
_flash flag is already set, for instance:
Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click If Not _flash Then _flash = True Dim FlashThread As New Thread(New ThreadStart(AddressOf FlashLabel)) FlashThread.Start() End If End Sub