I'm working on a project that will require me to react to 5 separate events generated by an external device and then do something after a certain delay. The events will normally happen one at a time but may occasionally be simultaneous. Is this a bad idea and if so why?
Imports System.Windows.Threading
Class MainWindow
Private TimerList As List(Of DispatcherTimer)
Private Sub Window_Loaded(ByVal sender As System.Object,
ByVal e As ystem.Windows.RoutedEventArgs) Handles MyBase.Loaded
TimerList = New List(Of DispatcherTimer)
'Create 5 timers for the 5 events from external device
For i As Integer = 1 To 5
TimerList.Add(
New DispatcherTimer(TimeSpan.FromSeconds(10), DispatcherPriority.Normal,
New System.EventHandler(AddressOf tmr_Tick),
Me.Dispatcher) With {.Tag = i}
)
Next
End Sub
Public Sub SomeEventFromExternalDevice(ByVal ID As Integer) 'handles...
Dim CurTimer = TimerList.Find(Function(x) x.Tag = ID)
If Not CurTimer Is Nothing Then
CurTimer.Start()
End If
End Sub
Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ID = DirectCast(sender.tag, Integer)
Dim curTimer = TimerList.Find(Function(x) x.Tag = ID)
curTimer.Stop()
Select Case ID
'change something on the UI to indicate that event x timer has elapsed
End Select
End Sub
End Class
Probably a better solution to this is to get rid of the DispatcherTimer altogether and do this with plain threads.
First create a class to hold a single event work package - I've added a way to pass the delay value here but you can omit it if it doesn't fit your needs. This is a simple procedure which sleeps the thread for the delay and then raises a new event for you to catch. If you need precision then you can implement the delay with a Stopwatch or something else:
Now your device will fire events handled here:
This way, each time an event fires you start a new thread which waits for your delay time, does DoSomething and then terminates, cleaning itself up in the process.
Here you will need some "DoSomething" procedure :
The DoSomething procedure will be called from each thread so it has to invoke on the main UI thread - then need :
If knowing the exact order of the events is important - knowing when they arrived and in what order - then you could add a timestamp in the SomeEventFromExtDevice and pass that along also.
You might also want to add some handling for shutting down the application - there are no checks here to make sure that threads are not trying to marshall onto the main form after it has been disposed, for example.