Read the syntax
Separate work from progress presentation and understand Progress callback dispatch.
This particular implementation writes synchronously, so the reports appear before the final message.
What it is and why to use it
A worker should not need to know how a progress bar or console message is drawn. IProgress<T> defines a Report operation that the caller can supply. The interface itself does not require asynchronous dispatch. Progress<T> is one implementation that captures the current SynchronizationContext at construction, making its creation location important in UI applications.
How it works
A custom implementation can report synchronously, while Progress<T> posts callbacks through its captured context or to the thread pool when no context was captured. Report is not an awaitable completion notification. Design a separate Task for work completion and avoid assuming every progress update has already reached the UI when the worker finishes.
Common mistakes and limitations
Do not update UI controls from arbitrary worker threads. Do not create Progress on a worker and assume it captured the UI context. Avoid flooding consumers with excessive updates, and do not rely on callback ordering across arbitrary custom implementations or concurrent reporters.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
static void Work(IProgress<int> progress) { progress.Report(50); }Examples
Supply a synchronous console progress sink
Show that the interface can have a straightforward synchronous implementation.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Work(new ConsoleProgress());
Console.WriteLine("Work finished");
static void Work(IProgress<int> progress) { progress.Report(50); progress.Report(100); }
sealed class ConsoleProgress : IProgress<int> {
public void Report(int value) => Console.WriteLine(value + "%");
}
- The worker depends only on IProgress<int>.
- This particular implementation writes synchronously, so the reports appear before the final message.
- Another implementation can dispatch differently. The interface alone does not promise this scheduling behaviour.
- Expected output: 50% 100% Work finished
- Keep work completion separate from progress presentation.
Wait explicitly for a dispatched callback
In a console context, observe a Progress callback without using a sleep.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
var observed = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously);
IProgress<int> progress = new Progress<int>(value => observed.TrySetResult(value));
progress.Report(75);
Console.WriteLine(await observed.Task);
- A normal console entry point has no UI SynchronizationContext, so Progress dispatches through the thread pool.
- The completion source records when the one demonstration callback actually runs.
- Awaiting that signal makes the output deterministic without claiming that Report itself waits for callback completion.
- Expected output: 75
- Keep work completion separate from progress presentation.
Best practices
- Keep work completion separate from progress presentation.
- Construct context-aware progress handlers on the intended context.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference