I have a timer that I have tied to the behavior of a UI element such that the UI element should auto-hide itself after 30s. After that point, it should only be made visible by explicitly calling Show
on the element. I have implemented like this:
Private Sub myBtn_VisibleChanged(sender As Object, e As EventArgs)
_myBtn.Enabled = True
_myTmr.Start()
End Sub
Private Sub myTmr_Elapsed(sender As Object, e As EventArgs)
_myBtn.Hide()
_myBtn.Enabled = False
_myTmr.Stop()
End Sub
_myBtn
has the VisibleChanged
event handled by the above method and _myTmr
is setup this way:
Dim _myTmr As System.Timers.Timer = New System.Timers.Timer(30000.0)
_myTmr.AutoReset = False
_myTmr.Enabled = False
AddHandler _myTmr.Elapsed, AddressOf myTmr_Elapsed
I have a few questions about this setup:
- Do I need to set
_myTmr.Enabled
toFalse
on initialization or is that the default behavior? I'm unsure about this and I was unable to track down documentation that elucidated on this point. - How does the
AutoReset
property work? Does setting it toFalse
mean that,Elapsed
will only be raised once? After that happens, what will the value of the timer be? Will callingStart
on_myTmr
again, causeElapsed
to be raised again? - It appears that
myTmr_Elapsed
throws anInvalidOperationException
because theElapsed
event is raised from a different thread than the UI thread. Is there a way to callHide
on the UI thread?