I asked a question yesterday and the answer was good. But now I'm trying to understand the role of await
and how task execution works.
I've read about await
that: The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work (from msdn site).
Task.run
: The Run method allows you to create and execute a task in a single method call and is a simpler alternative to the StartNew method (from msdn site).
Now, with the code:
public async Task YourFunc()
{
Exception error = null;
try
{
var task = Task.Run(() =>
{
Thread.Sleep(3000);
throw new ArgumentException("test argument exception");
});
var completed = task.IsCompleted;
var faulted = task.IsFaulted;
Console.WriteLine(completed);
Console.WriteLine(faulted);
}
catch (Exception ex)
{
error = ex;
}
this.MigrationProcessCompleted(error);
}
I've removed the await operator and I've set a breakpoint on the line Console.WriteLine(completed);
. Why even after 2-3 minutes of waiting in this breakpoint, the task is not completed and not faulted? I've set a breakpoint inside the task's code and exception is trown, so the task must be marked as faulted or completed at least...