How to Pause and Resume the Task which runs Asynchronously
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
An asynchronous task usually cannot be "paused" externally in the same way a debugger pauses a thread. In practice, pause and resume must be designed cooperatively into the task itself. The task checks a synchronization primitive at safe points, waits when paused, and continues when resumed.
Why Task Does Not Have Built-In Pause/Resume
In C#, Task represents an asynchronous operation, not a schedulable thread that the runtime can safely freeze and restart at arbitrary instructions. If .NET exposed a force-pause API, it would risk deadlocks, broken invariants, and paused code while holding locks.
That is why a pauseable async workflow is normally written as a loop that cooperates with a gate object.
A Practical Pattern With ManualResetEventSlim
One simple pattern is to use ManualResetEventSlim as a pause gate. When the gate is set, work continues. When the gate is reset, the task waits.
This works because the task checks the pause gate at a safe boundary inside the loop.
Making The Pause Fully Async-Friendly
ManualResetEventSlim.Wait blocks the current thread. For CPU-bound background work that may be fine, but for highly asynchronous code you may prefer a truly async gate built with SemaphoreSlim or a task-based signal.
A small async-friendly pattern is to wait on a TaskCompletionSource when paused and replace it when resumed. That avoids blocking a thread pool thread.
You would then call await pauseToken.WaitWhilePausedAsync() inside the worker loop.
Design The Task Around Safe Pause Points
The important design rule is that pausing should happen at boundaries where the operation can safely stop and later continue. Examples include:
- between work items in a queue
- between batches of file processing
- between iterations of a polling loop
Pausing in the middle of a transaction, while holding a lock, or after partially mutating shared state can create hard-to-debug failures. Cooperative pause only works well when the task structure already has natural checkpoints.
Cancellation Is A Different Concern
Pause and cancellation are not the same feature. Pausing means "wait and continue later." Cancellation means "stop and unwind now."
A robust design often supports both. The example above uses ManualResetEventSlim for pause/resume and CancellationTokenSource for shutdown.
Common Pitfalls
A common mistake is looking for a built-in Task.Pause() API. .NET does not provide one for general tasks.
Another mistake is pausing code while it holds locks or owns partially updated shared state. Resume may work, but the pause itself can block unrelated parts of the system.
Developers also sometimes block inside code that should stay fully asynchronous. If thread blocking matters, use an async-friendly pause token instead of Wait().
Finally, do not overload cancellation to mean pause. They are different lifecycle events and should be modeled separately.
Summary
- Pause and resume in async code must be cooperative.
- '
Taskdoes not support arbitrary external suspension.' - A
ManualResetEventSlimgate is a practical solution for many worker loops. - For non-blocking async flows, use an awaitable pause token pattern.
- Add pause checks only at safe boundaries in the workflow.
- Treat pause/resume and cancellation as separate behaviors.
Related reading
- How to perform an async task against es6 generators in loop
- How to perform async initalization of lazy injection
- How to perform multiple asynchronous requests starting one after another
- How to pick an event listener that will let me wait until async.times is finished to run a function
- How to play sounds asynchronuously, but themselves in a queue?
- How to preserve HttpContext in Web API async task
- How to process GuzzleHTTP async requests without blocking?
- How to promisify correctly JSON.parse method with bluebird
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.