19 comments

  1. I appreciate your efforts.

    Real Exams

  2. […] C# 5.0 Async explained as simple as possible […]

    Derek’s Dope List Archive | Derek's Crib

  3. Hi thanks for the post. Is there a way to cancel or stop tasks running with await in the middle of their execution?

    Carlo

    • Actually,it depends on the async method you are using. Most of the framework methods, like FileStream.WriteAsync allow you to pass a CancellationToken which you can you use to cancel the running method. The WriteAsync method monitors the token for cancellation requests and cancels accordingly. When you are writing your own async methods you are the one that should allow cancellation bij using an extra param for the token and monitoring the token.

      Chris van Beek

  4. Actually, it depends on the async method you are using. Most of the framework methods, like FileStream.WriteAsync allow you to pass a CancellationToken which you can you use to cancel the running method. The WriteAsync method monitors the token for cancellation requests and cancels accordingly. When you are writing your own async methods you are the one that should allow cancellation bij using an extra param for the token and monitoring the token.

    Chris van Beek

  5. Very good articles ..really understood in detial. Explained better than MSDN for sure.

    Dexter

  6. Thanks!

    Chris van Beek

  7. […] a look at this blog post, it gives a nice introduction and explanation of what async and await […]

    What is async and await and when would you use these in windows development? | PHP Developer Resource

  8. I appreciate your insight into it.

    Jing

  9. Thanks for this great explanation. Really nice to read this simplified version of the complex MSDN post.

    Tim D

  10. Wow!! I was really struggling to get await and async working.. Especially writing an async function myself was something I couldn’t get done. All I could find on the internet is examples where you call await …Async() function from a .NET framework object.

    Mark ter Luun

  11. Hi
    im working in visual studio 2012 c# , in my program i have method with async keyword..im trying to call normal method from the async method,it working but it doesn’t active another thread ( open the another page or application )
    here is my code ,please help me out

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {

    string text = await RecognizeTextSearchGrammar();
    if (text != null)
    {
    await Speak(string.Format(“Searching Application {0}”, text));

    result = text;
    match(result); —-> normal method ( trying to open the another application )
    }

    Thanks in advance

    Lavanya

    • It all depends on the implementation of the RecognizeTextSearch method and the Speak method. They should activate a background thread by starting a Task and returning that Task. Make sure you return a started task from those two methods. Just using async and await is not enough to perform multi threading. Does this answer your question? If not then I need to undertand in which part of this method you are missing a background thread.

      Chris van Beek

      • Hi Chris van Beek,
        yeah I can understand and now my code working perfectly. Thanks lot for your immediate response .really you blog is simply superb with excellent explanation.

        Lavanya

        • Wow, thanks for your kind words. I am glad I could be of help :).

          Chris van Beek

  12. Can you please explain a little bit more about

    result = Calculate(number1, number2).Result;

    As this, btnCalculate_Click() doesn’t have to declare with async. However, does this still work asynchronous?
    And does the compiler split the code with this call?
    Do we have any callback in this way?

    Vu Nguyen

    • I am assuming that you mean the first code sample? You are right this is entirely synchronous code. This example shows the problem. Later on in the article you will find the solution to make it asynchronous and you will see that the eventhandler also get’s the async keyword.

      Chris van Beek

Comments are closed.