I have a console application that I would like to keep open all of the time while still listening in to events. I have tested Thread.Sleep(Timeout.Infinite);
and while (true) { }
and both allow the events to be raised while keeping the console application open. Is there one that I should be using over the other? If the thread is sleeping, is there anything that I should not be doing, such as modifying a static collection declared in the scope of the class?
Make it wait for a mutex or resource that will never be released. It's the deadlock principle. The better way is to make the thread to finish his code, so it will end and not be started again.
Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.
Thread. sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.
Sleep causes thread to suspend itself for x milliseconds while yield suspends the thread and immediately moves it to the ready queue (the queue which the CPU uses to run threads).
I would recommend using a ManualResetEvent
(or other WaitHandle
), and calling ManualResetEvent.WaitOne.
This will have a similar effect to sleeping forever, except that it provides you a clean way to exit from your infinite "block" when desired (by calling Set()
on the event).
Using while(true)
will consume CPU cycles, so it's definitely something to avoid.
is there anything that I should not be doing, such as modifying a static collection declared in the scope of the class?
In general, no. Since your thread will be blocked, there shouldn't be any synchronization issues with using shared data (provided the items within the collection don't have specific requirements, such as user interface elements which must be used on a thread with a proper synchronization context.)
Since C# 7.1, you can make the Main method asynchronous. That means instead of using busy-wait or thread-locking sleeping, you can suspend the Main method asynchronously as a Task. This way, the thread which ran Main will not be locked. And with Cts.Cancel()
, you can easily release the main task to exit the application (without allowing work for other threads/tasks to finish).
static readonly CancellationTokenSource Cts = new CancellationTokenSource(); static async Task Main(string[] args) { /* your code here */ // Task running Main is efficiently suspended (no CPU use) forever until Cts activated with Program.Cts.Cancel(); (thread-safe) from anywhere. await Task.Delay(Timeout.Infinite, Cts.Token).ConfigureAwait(false); }
Since year 2020, C# 9.0, the whole Program.cs
file content can literally look exactly like this:
using System.Threading; using System.Threading.Tasks; CancellationTokenSource Cts = new(); /* your code here */ // Task running Main is efficiently suspended (no CPU use) forever until // Cts activated with Program.Cts.Cancel(); (thread-safe) from anywhere. await Task.Delay(Timeout.Infinite, Cts.Token).ConfigureAwait(false);
You can try-catch TaskCanceledException if infinite-sleep cancellation is normal codeflow for the case (please DON'T use Task.Delay
inf-sleep when you are trying to await/sync with other Tasks, try-catch is less efficient and will confuse teammates; use other mechanisms for task cooperation):
try { await Task.Delay(Timeout.Infinite, Cts.Token).ConfigureAwait(false); } catch(TaskCanceledException) { /* Ignored */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With