Recently I try to learn about the new C# feature, async/await keywords for asynchronous programming. When I searched at web, I faced this example:
static void Main(string[] args)
{
Console.WriteLine("Task based APM demo");
// Call Exponnent() asynchronously.
// And immediately return the control flow.
// If I don't put a Task here, the program will sometimes
// terminate immediately.
Task t = new Task(async () =>
{
int result = await Program.Exponent(10);
// After the operation is completed, the control flow will go here.
Console.WriteLine(result);
});
t.Start();
Console.ReadKey();
}
static async Task<int> Exponent(int n)
{
Console.WriteLine("Task started");
return await TaskEx.Run<int>(() => 2 << (n - 1));
}
}
I have questions about it and about the manner that this statements act. First as I understand an await expression used when we want release the process at that point and return to the caller context. But why this expression used this at the line which the Exponent method called at it? In fact what happen when the compiler faced this line of program? And the letter question is, why the program use "TaskEx.Run" for returning the result in the body of Exponent method? Is it possible to use "return await (() => 2 << (n - 1));" only? How the compiler behaves with this line?
Thanks in advance
But why this expression used this at the line which the Exponent called at it?
I'm not sure what you mean by this question. await Task.Run<int>(() => 2 << (n - 1));
tells the compiler to suspend execution of the current method until the task completes. In terms of task, this is similar to:
Task.Run<int>(() => 2 << (n - 1)).ContinueWith(t=>return t.Result);
But, of course, you cannot return
from a continuation. The async
keyword tells the compiler not only that there could be one or more await
operators in the method, but set's up a state machine to manage the different state transitions starting and returning from asynchronous operations. As well, this could affect how the method is executed in relationship to being asynchronous to the caller.
As to the Task
used in Main
: you can't await
a method call in Main
, so, I assume the Task
is used to still use the await
operator. But, it seems redundant. I would probably simply have another method to use await
:
static async void Method()
{
Console.WriteLine(await Exponent(10));
}
Then re-write Main
:
static void Main(string[] args)
{
Method();
Console.ReadLine();
}
But, I don't know the reasons behind why the code was originally written that way or even if there was a real purpose.
Is it possible to use "return await (() => 2 << (n - 1));"
It is not possible to write this line of code because nothing asynchronous is being performed. () => 2 << (n - 1)
neither returns a Task object not has an GetAwaiter
method associated with it; so, it can be executed asynchronously. thus, it can't be "awaited".