public void Dowork();
public async Task PerformOperationAsync() {
//Call here the
//synchronous method
//Dowork()
//asynchronously;
}
Mphasis · Role not specified · technical
Published
Year not specified · C#
Community answers
Posted by wasAsked editorial collection
Do not wrap naturally asynchronous I/O in Task.Run; expose and await its async API. Task.Run is appropriate mainly for CPU-bound or unavoidable blocking work that you intentionally move off a UI thread.
Example:
public Task<Result> DoWorkAsync(CancellationToken ct) =>
Task.Run(() => DoWork(ct), ct);
In ASP.NET Core, Task.Run does not make blocking work scalable: it still consumes a thread-pool thread. Prefer replacing the synchronous dependency, or run long work in a bounded background queue and return a job identifier. Pass cancellation, observe exceptions, and never fire-and-forget request-scoped work.
Backbencher Developer · · 0 likes
Two correct answers depending on what DoWork is.
If DoWork is CPU-bound or a blocking call you cannot change, and you need to avoid blocking the caller (for example in a desktop UI), offload it to the thread pool:
public void DoWork()
{
// synchronous, blocking work
}
public async Task PerformOperationAsync(CancellationToken ct = default)
{
await Task.Run(() => DoWork(), ct);
// continue after DoWork has completed, without having blocked the calling thread
}
If DoWork returns a value: int result = await Task.Run(() => Compute(), ct);
What Task.Run does: it queues DoWork to run on a thread-pool thread and returns a Task you can await. The calling thread (for example the UI thread) stays free.
Important caveats interviewers expect you to mention:
1. Task.Run does not make DoWork itself asynchronous — it still blocks a thread, just a different one. It is useful for keeping UI responsive or running CPU work in parallel, not for scalability.
2. In ASP.NET Core request handlers, avoid wrapping synchronous code in Task.Run. The request is already on a thread-pool thread, so Task.Run only adds overhead and uses an extra thread. Call it directly, or better, make it truly asynchronous.
3. The better fix when DoWork does I/O (database, HTTP, file) is to rewrite it with async APIs:
public async Task DoWorkAsync(CancellationToken ct)
{
await using var conn = new SqlConnection(_connString);
await conn.OpenAsync(ct);
// ExecuteNonQueryAsync, ReadAsync, HttpClient.GetAsync, File.ReadAllTextAsync...
}
public Task PerformOperationAsync(CancellationToken ct) => DoWorkAsync(ct);
This frees the thread entirely while waiting for I/O.
4. Do not use "async void" or fire-and-forget (Task.Run(() => DoWork()); without awaiting) unless you deliberately handle exceptions and lifetime — unobserved exceptions are lost and work may be cut off when the app shuts down.
5. Long-running dedicated work can use Task.Factory.StartNew(DoWork, TaskCreationOptions.LongRunning) to avoid tying up a pool thread, but this is rarely needed.
Summary answer: "await Task.Run(DoWork) if it is CPU-bound and I need to keep the caller responsive; if it is I/O-bound, I would convert it to a real async method instead."