Task.Run is not clarified enough

Most of the examples that you come across for Task.Run, are forbidding its use. There are many reasons for using it in a web app besides a background task. You might have a legacy code which can't be converted to async. So, to run it asynchronously, enclose it inside a Task.Run. For example -

function Customer GetCustomerDetails(int customerId)
{
var customer = GetCustomerById(1);

var ordersTask = GetOrdersByCustomerIdAsync(1);

var addressesTask = GetAddressesByCustomerIdAsync(1);

var reportsTask = Task.Run(() => GetReportsByCustomerId(1));

await Task.WhenAll(ordersTask, addressesTask, reportsTask);

var customerDetails = new Customer();

customerDetails.Orders = ordersTask.Result;

customerDetails.Addresses = addressesTask.Result;

customerDetails.Invoices = reportsTask.Result;

return customerDetails;
}






 

Add comment