vote buttons
0
1
beginner
0
intermediate
0
advanced
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.
16-Nov-2014 02:26 UTC
user0172
0

1 Answers

vote buttons
0

As you mentioned it's a console application, simplest way would be to use Console.ReadKey() instead of the while loop. This way the program would continue executing till someone presses a key.

Another alternative is to use AutoResetEvent. Declare an AutoResetEvent

AutoResetEvent arEvent = new AutoResetEvent(false);

and call 

arEvent.WaitOne()

in place of the while loop. The main thread would then continue waiting without consuming cpu till it receives a signal. You can call

arEvent.Set()
when you want to exit the application from anywhere in the code.
16-Nov-2014 03:53 UTC
K Bonneau
350
Related Questions