Reputation:
I am aware that in .NET there are three timer types (see Comparing the Timer Classes in the .NET Framework Class Library). I have chosen a threaded timer as the other types can drift if the main thread is busy, and I need this to be reliable.
The way this timer works in the control of the timer is put on another thread so it can always tick along with the work begin completed on the parent thread when it is not busy.
The issue with this timer in a console application is that while the timer is ticking along on another thread the main thread is not doing anything so the application closes.
I tried adding a while true
loop, but then the main thread is too busy when the timer does go off.
Upvotes: 5
Views: 1086
Reputation: 1069
I would use a Barrier that waits until all threads are done. Since there is a main and a counting thread, I have 2 participants. So it will look something like this:
Module Module1
Dim x As New Threading.Thread(AddressOf tick)
Dim y As New Threading.Barrier(2)
Sub Main()
x.IsBackground = True
x.Start()
y.SignalAndWait()
End Sub
Sub tick()
Threading.Thread.Sleep(10000)
y.SignalAndWait()
End Sub
End Module
Upvotes: 4