I am new to c# and trying to understand the difference between following two implementations. Looks like they both are doing exactly same and the output is exactly same. If both cases are invoking the run method synchronously, what value does async await really providing? Or there is really no benefit here?
private async Task<int> MyTask(CancellationToken cancellationToken)
{
Log.Debug("Task started");
await Task.Delay(5000, cancellationToken);
Log.Debug("Task ended");
return 0;
}
public async Task<int> Run()
{
var result = await MyTask(_cancellationToken);
return result;
}
This how Run method is called
var returnCode = jobRunner.Run().Result;
And this implementation?
private int MyTask(CancellationToken cancellationToken)
{
Log.Debug("Task started");
var task = Task.Delay(5000, cancellationToken);
task.Wait(cancellationToken);
Log.Debug("Task ended");
return 0;
}
public int Run()
{
var result = MyTask(_cancellationToken);
return result;
}
This how Run method is called in this case
var returnCode = jobRunner.Run();