---
title: "How to apply a Timeout strategy using Polly in C#?"  
description: "How to apply a Timeout strategy using Polly in C#?"  
author: "Uttam Misra"  
published: 2026-08-14  
updated: 2026-08-14  
canonical: https://answers.mindstick.com/qa/117059/how-to-apply-a-timeout-strategy-using-polly-in-c  
category: "Polly"  
tags: ["csharp", "dotnet", "polly", "async", "timeout"]  
reading_time: 1 minute  

---

# How to apply a Timeout strategy using Polly in C#?

Long-running requests can consume system resources and cause bottleneck issues. Applying a Timeout strategy ensures that calls return control promptly if a dependency takes too long to respond.

## Timeout Implementation in Polly v8

Polly supports both optimistic and pessimistic timeout strategies. Optimistic timeout relies on `CancellationToken` cancellation support.

### Code Example

```cs
var pipeline = new ResiliencePipelineBuilder()
    .AddTimeout(new TimeoutStrategyOptions
    {
        Timeout = TimeSpan.FromSeconds(3),
        OnTimeout = args =>
        {
            Console.WriteLine($"Operation timed out after {args.Timeout.TotalSeconds}s");
            return ValueTask.CompletedTask;
        }
    })
    .Build();

try
{
    await pipeline.ExecuteAsync(async cancellationToken =>
    {
        await LongRunningTaskAsync(cancellationToken);
    });
}
catch (TimeoutRejectedException)
{
    // Handle timeout scenario
}
```


---

Original Source: https://answers.mindstick.com/qa/117059/how-to-apply-a-timeout-strategy-using-polly-in-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
