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?

- 3,164
- 6
- 30
- 46
6 Answers
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.)

- 554,122
- 78
- 1,158
- 1,373
-
I actually don't want it to exit at all, but I will accept your answer in case that there are people looking that do wish to have a way to exit. – Lunyx Nov 15 '12 at 21:39
-
@Daniel It is nice to have a clean way for your program to shutdown *eventually*, even if that's not something you want at any specific point in time ;) That being said, I'd still use this approach over a sleep, personally. – Reed Copsey Nov 15 '12 at 21:50
-
How would using using the ManualResetEvent differ with Thread.Sleep() if the only time that the program closes is when the machine restarts? – Lunyx Nov 15 '12 at 21:52
-
2@Daniel I think that the intention is a bit more clear. It's going to have the same effect – Reed Copsey Nov 15 '12 at 22:13
-
The ManualResetEvent option takes a few more characters and the intent is no clearer than "sleep for infinity", but it does *feel* less of a hack. – Kevin Coulombe May 18 '13 at 05:00
-
If you want asynchronously suspend code using Task indefinitely, my answer is better. – Vinigas Jan 17 '21 at 07:04
-
@ReedCopsey, can u give sample example code for this, ManualResetEvent vs while-loop true – Ichigo Kurosaki Aug 29 '23 at 23:18
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 */ }

- 464
- 5
- 18
-
1
-
1`Cts.Cancel();` https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.cancel?view=net-5.0#System_Threading_CancellationTokenSource_Cancel – Vinigas Dec 13 '20 at 09:58
-
2
I think the call
while (true) { ... }
is computationally intensive, since the thread never stops, wheareas
Thread.Sleep(Timeout.Infinite);
actually gets the thread to sleep with help of OS native schedulers. And then the thread actually stops, so I suppose it's less computationally demanding.

- 36,151
- 76
- 250
- 438

- 407
- 6
- 11
-
4There seems uncertainty in your answer... "I think", "I suppose", etc. – Lee Taylor Nov 15 '12 at 21:51
-
1i was sure about the answer but i just started a career in java that is why i used "I think", "I suppose" for precaution. :) – leoismyname Nov 16 '12 at 00:51
Unlike while(true)...
, Thread.Sleep
does not use CPU cycles, so in this sense, the sleep is more efficient. In general, using Busy Waiting outside of spinlocks is strongly discouraged.
If the thread is sleeping, is there anything that I should not be doing?
Since your thread is blocked upon entry to Thread.Sleep
, anything that you wish to do to its resources is a fair game.

- 714,442
- 84
- 1,110
- 1,523
-
Thanks, I've seen the while loop being recommended to people trying to accomplish what I'm doing and I wasn't sure if that was the correct way to go about it. – Lunyx Nov 15 '12 at 21:31
Yes,
while(true)
consumes CPU while sleep()
works in a smarter way:
The sleep()
function puts the current execution context to sleep; it does this by calling a syscall to invoke the kernel sleep function which atomically
(a) sets a wake-up timer
(b) marks the current process as sleeping
(c) waits until the wakeup-timer fires or an interrupt occurs
If you call sleep()
, the CPU can do other work.
That's one reason why sleep()
is useful.
A useful link - Be careful when using Sleep

- 39,734
- 6
- 101
- 123

- 57,103
- 20
- 141
- 208
Calling the Thread.Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread.