Refactoring Backgroundworker to async/await
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
BackgroundWorker was a common way to keep desktop applications responsive before task-based async became standard in .NET. Today, async and await usually produce simpler code, better cancellation support, and cleaner exception handling. Refactoring is not just syntax cleanup; it is a shift from event-driven threading to task-based asynchronous flow.
Why BackgroundWorker Feels Heavy
BackgroundWorker splits one operation across several event handlers:
- '
DoWork' - '
ProgressChanged' - '
RunWorkerCompleted'
That works, but it scatters the logic and makes data flow harder to follow. Error handling is also awkward because exceptions must cross event boundaries instead of being awaited naturally.
A typical legacy pattern looks like this:
This is functional, but the operation is fragmented.
The async and await Replacement
With task-based async, the long-running method becomes a task-returning method, and the UI event handler awaits it directly:
This version keeps the UI responsive without creating manual event choreography. The code reads top to bottom, which is the main benefit of the refactor.
Map Old Concepts to New Ones
When converting BackgroundWorker, translate each concern explicitly:
- background execution becomes
TaskorTask<T> - completion callback becomes
await - progress reporting becomes
IProgress<T> - cancellation becomes
CancellationToken
Here is a progress and cancellation example:
That is the direct modern equivalent of WorkerReportsProgress and WorkerSupportsCancellation, but with much better composability.
CPU-Bound Versus I/O-Bound Work
Do not mechanically replace everything with Task.Run. If the work is I/O-bound, such as HTTP, database, or file access, prefer native async APIs and await them directly.
Use Task.Run mainly when:
- the code is CPU-heavy
- the API is synchronous and cannot be changed yet
- you are in a desktop UI and need to move blocking work off the UI thread
That distinction matters because fake async built from Task.Run is not the same as true non-blocking I/O.
Refactoring Strategy
The safest migration path is incremental:
- extract the long-running logic into a separate method
- convert that method to
TaskorTask<T> - replace completion handlers with
await - add
CancellationTokenandIProgress<T>only where needed
This avoids rewriting the entire screen at once.
Common Pitfalls
The most common mistake is blocking on async code with .Result or .Wait(). In UI applications, that can deadlock or freeze the interface.
Another mistake is using async void everywhere. Only UI event handlers should usually be async void; reusable methods should return Task.
A third issue is wrapping already-async APIs in Task.Run, which adds thread-pool overhead without solving a real problem.
Summary
- '
BackgroundWorkercan usually be replaced with task-based async code.' - '
awaitremoves the need for separate completion handlers.' - Use
IProgress<T>for progress andCancellationTokenfor cancellation. - Prefer native async APIs for I/O-bound work and reserve
Task.Runfor CPU-bound work. - Refactor incrementally so behavior stays stable while the code becomes easier to maintain.
Related reading
- reference assignment is atomic so why is Interlocked.Exchangeref Object, Object needed?
- Refreshing UITableView Asynchronously after Core Data Loaded Swift
- Regarding usage of Task.Start , Task.Run and Task.Factory.StartNew
- Reliably stop System.Threading.Timer?
- Reference a .NET Core Library in a .NET 4.6 project
- Reference Microsoft.SqlServer.Smo.dll
- Repeatedly prompt user until resolved using nodeJS async-await
- replication between SQL Server and MySQL server

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the 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.