Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Infinite Loop: Causes, Detection, and Prevention

Understand how infinite loops occur in C#, how to break out of them, how to debug them, and how to prevent them in production code.

~5 min readC#
#infinite loop#while loop#for loop#debugging
Illustration of an infinite loop in C# with a while condition that never becomes false.

A common way to accidentally create a C# infinite loop is to write a while loop whose condition never becomes false. For example, while (true) { ... } runs forever unless the loop body explicitly breaks out. This article explains how infinite loops occur in C#, how to avoid them, and how to debug them when they appear in production.

The Most Common Infinite Loop Patterns

The simplest infinite loop is a while loop with a constant condition:

csharp
while (true) { // Do work }

A for loop with no exit condition behaves the same way:

csharp
for (;;) { // Do work }

Both forms are intentional when you need a loop that runs until an external event occurs, but they become bugs when the exit condition is never reached. A more subtle pattern is a loop whose condition depends on a variable that is never updated:

csharp
int counter = 0; while (counter < 10) { Console.WriteLine(counter); // counter is never incremented }

Here the loop body does not change counter, so the condition counter < 10 remains true forever. The same problem appears in for loops when the increment step is missing or conditional.

Recursion can also produce an infinite loop when the base case is unreachable:

csharp
void Recurse() { Recurse(); }

This call stack grows until the process runs out of stack space, which usually throws a StackOverflowException, but the behavior is still an uncontrolled loop.

How to Break Out of an Infinite Loop

The break statement exits the closest enclosing loop or switch block. It is the most direct way to stop a loop when a condition is met:

csharp
while (true) { var input = Console.ReadLine(); if (input == "exit") { break; } Process(input); }

The return statement exits the entire method, which also stops the loop. This is useful when the loop is inside a method that should finish when a certain result is produced:

csharp
int FindFirstPositive(int[] numbers) { for (int i = 0; i < numbers.Length; i++) { if (numbers[i] > 0) { return i; } } return -1; }

Throwing an exception also exits the loop, but it should be reserved for exceptional conditions, not normal control flow.

When the loop condition is based on a variable, you can set that variable to a value that makes the condition false:

csharp
bool running = true; while (running) { // ... if (shutdownRequested) { running = false; } }

This approach is often clearer than break because the exit condition is part of the loop's state.

Debugging an Infinite Loop

The first symptom of an infinite loop is usually a process that consumes 100% of a CPU core and never returns. In Visual Studio, you can break execution with Debug > Break All or by pressing Ctrl+Alt+Break. The debugger pauses the process and shows the current call stack, which often points directly at the loop.

Placing a breakpoint inside the loop body lets you inspect variable values and see why the condition never becomes false. If the loop runs millions of times per second, you may need to add a conditional breakpoint that triggers after a certain number of iterations:

csharp
int iteration = 0; while (true) { iteration++; // Set a conditional breakpoint on the next line: iteration == 100000 DoWork(); }

Logging the loop counter or relevant state to a file can help when the issue only appears in production and you cannot attach a debugger. Be careful with logging inside a tight loop because it can slow the process and generate enormous log files.

Preventing Infinite Loops in Production Code

A defensive approach is to add an iteration limit to loops that are not supposed to run indefinitely. For example, a retry loop should stop after a fixed number of attempts:

csharp
int maxAttempts = 5; int attempt = 0; bool success = false; while (!success && attempt < maxAttempts) { attempt++; success = TryOperation(); }

For loops that process user input or external data, validate that the input can actually make progress. If the loop reads from a stream, check that the stream position advances. If it reads from a collection, ensure that the collection is not modified in a way that prevents termination.

Cancellation tokens are the standard way to stop long-running loops in modern C#. They allow cooperative cancellation from another thread or from a user request:

csharp
while (!cancellationToken.IsCancellationRequested) { // Do work await Task.Delay(100, cancellationToken); }

The loop checks the token before each iteration. If cancellation is requested while the loop is waiting in Task.Delay, that call throws OperationCanceledException, so handle it where the loop exits if you need a clean shutdown path. This pattern is common in background services and worker processes.

Performance and Resource Impact

An infinite loop that does not yield control can starve other threads and make the application unresponsive. In a single-threaded UI application, the UI thread will freeze. In a server process, a CPU-bound infinite loop can consume a core indefinitely, increasing latency for other requests.

If the loop allocates memory each iteration, it can also cause high garbage collection pressure. For example:

csharp
while (true) { var data = new byte[1024]; // ... }

This allocates a new array every iteration, which the garbage collector must eventually reclaim. Over time, this can lead to frequent GC pauses and increased memory usage. The loop itself may not be infinite by design, but a missing exit condition turns a normal allocation pattern into a resource leak.

When an Infinite Loop Is Intentional

Some applications require a loop that runs until shutdown. A game loop, a message pump, or a background worker often uses while (true) or while (!cancellationToken.IsCancellationRequested). The key difference is that these loops have a clear exit path, either through a break condition, a cancellation token, or an exception that terminates the process.

Even in intentional infinite loops, you should provide a way to stop the loop gracefully. A service that runs forever should respond to shutdown signals. A game loop should allow the player to quit. A message pump should exit when the application closes. Without an exit path, the loop becomes a bug that is difficult to stop without killing the process.

Using a Timeout to Detect a Stuck Loop

In some scenarios, you can use a timeout to detect that a loop is not making progress. This is useful when the loop is expected to finish within a bounded time. The CancellationTokenSource.CancelAfter method schedules a cancellation after a specified duration. For example, a loop that checks the token can be stopped after five seconds:

csharp
var cts = new CancellationTokenSource(); cts.CancelAfter(TimeSpan.FromSeconds(5)); try { await WorkLoopAsync(cts.Token); } catch (OperationCanceledException) { // The loop did not finish in time } async Task WorkLoopAsync(CancellationToken token) { while (!token.IsCancellationRequested) { await Task.Delay(100, token); DoWork(); } }

The token only helps if the loop observes it. A CPU-bound loop that never checks the token cannot be interrupted by CancelAfter alone; the loop must be written to cooperate. This technique is a practical timeout for cooperative loops, but it does not replace a correct exit condition.

Common Mistakes That Lead to Infinite Loops

A frequent mistake is comparing floating-point numbers for equality in a loop condition. Floating-point arithmetic often produces values that are not exactly representable, so a loop like while (x != 1.0) may never terminate. Use a tolerance or an integer counter instead.

Another mistake is modifying the loop variable inside the loop in a way that skips the increment. For example:

csharp
for (int i = 0; i < 10; i++) { if (condition) { i = 0; // resets the loop } }

This can cause the loop to restart indefinitely if the condition is always true. The same applies to while loops where the condition variable is reset inside the body.

Finally, be careful with foreach loops over collections that are modified during iteration. The foreach loop itself does not become infinite, but modifying the collection can cause an InvalidOperationException or undefined behavior. The infinite loop risk is more common with while loops that use an index and modify the collection size.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.