Long Running Task C#

Await and async in .NET 4.5 with C#

public Task<string> RunSlowOperationTask()
        {
            return Task.Factory.StartNew<string>(RunSlowOperation);
        }
OR
var taskData = new CalcTaskData {ForDate = forDate.Value, param2 = Value2};
 Task t = new Task(() => DoCalcAsync(taskData));
 t.Start();
// Long running Task
private async Task DoCalcAsync(CalcTaskData calcTaskData)
{
// long operation
// simulate long wait  (see note below)
}

Note that Thread.Sleep was replaced by Task.Delay(2000).

Thread.Sleep blocks the thread whereas Task.Delay represents a work that will block the thread for two seconds
Leave a Reply