I have a button-click event handler that among other things updates a private, non-shared instance variable in the containing Form.
I also have a System.Windows.Forms.Timer, whose Tick event comes around a few seconds after that button click event has completed.
My question: Why does the Tick event handler sometimes (quite often) see the previous value of that instance variable? (I thought that System.Windows.Forms.Timer is thread-safe with regard to instance variables.)
Related question: Is it relevant that this happens frequently on a very fast quad-processor computer but rarely if ever on a slow dual-processor? In other words, is it possible that the issue has something to do with synchronizing instance variables across CPUs?
Code follows. Comment conventions modified for display beauty.
/* Instance variable get/set */
Public Property mode() As modetype
Get
Return _mode
End Get
Set(ByVal value As modetype)
_mode = value
Select Case value
/* Lots of mode-specific processing here */
End Select
Debug.Assert(mode = value)
End Set
End Property
/* Click event handler */
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
Debug.Assert(Not (picVideo Is Nothing))
mode = modetype.clear
End Sub
/* Tick event handler */
Private Sub tmrCapture_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrLiveImageCapture.Tick
// FOLLOWING LINE is where mode should be .clear but comes up as .live instead.
If mode = modetype.live Then
Debug.Assert(mode = modetype.live) // Seriously? Yes.
Try
execute_timer_tick_stuff()
Catch ex As Exception
/* Shouldn't happen */
tmrLiveImageCapture.Stop() // FIXME: can you stop a timer within its own tick event?
MessageBox.Show("Error in live timer tick: " & ex.Message)
Debug.Assert(Not tmrLiveImageCapture.Enabled)
End Try
End If
End Sub
Thanks.