<p>I am aware that in <a href="http://en.wikipedia.org/wiki/.NET_Framework" rel="noreferrer">.NET</a> there are three timer types (see <em><a href="http://msdn.microsoft.com/en-us/magazine/cc164015.aspx" rel="noreferrer">Comparing the Timer Classes in the .NET Framework Class Library</a></em>). 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.</p>

<p>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.</p>

<p>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 to the application closes.</p>

<p>I tried adding a <code>while true</code> loop, but then the main thread is too busy when the timer does go off.</p>

[{'answer_id': 44, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 5, 'selected': False, 'text': '<p>Consider using a <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.manualresetevent" rel="noreferrer">ManualResetEvent</a> to block the main thread at the end of its processing, and call <code>Reset()</code> on it once the timer\'s processing has finished.  If this is something that needs to run continuously, consider moving this into a service process instead of a console app.</p>\n'}, {'answer_id': 45, 'author': 'huseyint', 'author_id': 39, 'author_profile': 'https://Stackoverflow.com/users/39', 'pm_score': 7, 'selected': True, 'text': '<p>You can use something like <code>Console.ReadLine()</code> to block the main thread, so other background threads (like timer threads) will still work. You may also use an <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.autoresetevent" rel="noreferrer">AutoResetEvent</a> to block the execution, then (when you need to) you can call Set() method on that AutoResetEvent object to release the main thread. Also ensure that your reference to Timer object doesn\'t go out of scope and garbage collected.</p>\n'}, {'answer_id': 65238796, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>According to <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer?view=net-5.0" rel="nofollow noreferrer">MSDN</a> and the other answers, a minimal working example of a Console application using a System.Threading.Timer without exiting immediately :</p>\n<pre><code>private static void Main()\n{\n    using AutoResetEvent autoResetEvent = new AutoResetEvent(false);\n    using Timer timer = new Timer(state =&gt; Console.WriteLine(&quot;One second has passed&quot;), autoResetEvent, TimeSpan.Zero, new TimeSpan(0, 0, 1));\n    autoResetEvent.WaitOne();\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/39', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/33/']