Only assigning method with async
don't make it to call asynchronously.For asynchronously calling it need a method invokation with the await
keyword in it's body.
And about this:
A void returning async method cannot be awaited; it is a "fire and
forget" method. It does work asynchronously...
This means that you can not await THIS method inside another method like this.
await Form1_KeyPressed(this, EventArgs.Empty)
For your code to work you need a method with await
keyword like:
private async void Form1_KeyPressed(object sender, EventArgs e)
{
for(int i=0; i<int.max; i++)
;
// In the body some code like this
await YourMethod();
}
Updated version
The “Async” Keyword
What does the “async” keyword do when applied to a method?
When you mark a method with the “async” keyword, you’re really telling the compiler two things:
- You’re telling the compiler that you want to be able to use the “await” keyword inside the method (you can use the await keyword if and only if the method or lambda it’s in is marked as async). In doing so, you’re telling the compiler to compile the method using a state machine, such that the method will be able to suspend and then resume asynchronously at await points.
- You’re telling the compiler to “lift” the result of the method or any exceptions that may occur into the return type. For a method that returns Task or Task, this means that any returned value or exception that goes unhandled within the method is stored into the result task. For a method that returns void, this means that any exceptions are propagated to the caller’s context via whatever “SynchronizationContext” was current at the time of the method’s initial invocation.
Does using the “async” keyword on a method force all invocations of that method to be asynchronous?
No. When you invoke a method marked as “async”, it begins running synchronously on the curren thread. So, if you have a synchronous method that returns void and all you do to change it is mark it as “async”, invocations of that method will still run synchronously. This is true regardless of whether you leave the return type as “void” or change it to “Task”. Similarly, if you have a synchronous method that returns some TResult, and all you do is mark it as “async” and change the return type to be “Task”, invocations of that method will still run synchronously.
Marking a method as “async” does not affect whether the method runs to completion synchronously or asynchronously. Rather, it enables the method to be split into multiple pieces, some of which may run asynchronously, such that the method may complete asynchronously. The boundaries of these pieces can occur only where you explicitly code one using the “await” keyword, so if “await” isn’t used at all in a method’s code, there will only be one piece, and since that piece will start running synchronously, it (and the whole method with it) will complete synchronously.
For more see here:
http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/async-await-faq.aspx