0

This is my first time to learn asynchronous method, i am still feel confused about the conversion method with use async and await, any good idea to help? The code below is the code for synchronous method.

using System;
using System.Threading.Tasks;

namespace TestingAsync
{
  class Program
  {
    static void Main(string[] args)
    {
        Console.WriteLine("start");
        
        Bash();
        WashClothes();// Washing clothes in the washing machine
        Usedryer();// Dry the clothes with a dryer
        Cooking();// Cooking dinner
        Eating();// Eating dinner
        Reading();// Reading article
        Writing();// Writing report
        
        Console.ReadKey();
    }

    private static void Bash()
    {
        Console.WriteLine("Start Bash");
        Task.Delay(2000).Wait();
        Console.WriteLine("End Bash");
    }

    private static void WashClothes()
    {
        Console.WriteLine("Start Wash clothes");
        Task.Delay(3000).Wait();
        Console.WriteLine("End Wash clothes");
    }

    private static void Usedryer()
    {
        Console.WriteLine("Start Drying clothes");
        Task.Delay(4000).Wait();
        Console.WriteLine("End Drying clothes");

    }

    private static void Cooking()
    {
        Console.WriteLine("Start Cooking");
        Task.Delay(2000).Wait();
        Console.WriteLine("End Cooking");
    }
    private static void Eating()
    {
        Console.WriteLine("Start Eating");
        Task.Delay(1000).Wait();
        Console.WriteLine("End Eating");
    }
    private static void Reading()
    {
        Console.WriteLine("Start Reading");
        Task.Delay(2000).Wait();
        Console.WriteLine("End Reading");
    }
    private static void Writing()
    {
        Console.WriteLine("Start Writing");
        Task.Delay(2000).Wait();
        Console.WriteLine("End Writing");
    }

   }
}
Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • Try making all the methods (except Main) be `async` and return a `Task`. Make `Main`, `async void`. Then `await` every call, including the calls to `Task.Delay`. By making everything async and awaiting it, you end up with an asynchronous state machine l – Flydog57 May 31 '21 at 03:07
  • 1
    @Flydog57 [avoid async void](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void). It's not a valid `Main` [entry point](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/main-and-command-args/). – Theodor Zoulias May 31 '21 at 07:58

2 Answers2

1
static async Task Main(string[] args)
{
    Console.WriteLine("start");
    
    await BashAsync();
    await WashClothesAsync();// Washing clothes in the washing machine
   
    ...
    
    Console.ReadKey();
}

private static async Task BashAsync()
{
    Console.WriteLine("Start Bash");
    await Task.Delay(2000);
    Console.WriteLine("End Bash");
}

private static async Task WashClothesAsync()
{
    Console.WriteLine("Start Wash clothes");
    await Task.Delay(3000);
    Console.WriteLine("End Wash clothes");
}
  • Swap void for async Task
  • Swap methods that return type X for async Task<X>
  • Remove .Wait() and add await on your delays
  • Add await on your method calls in main
  • Rename methods that return a Task to be called ...Async

If you get an error when you swap Main to return a Task, use a newer version of C#

If you’re struggling to understand Task Async Pattern (what this is) then in short:

Methods that have Async at the end of the name generally return something you can “call await” on and the code will wait for the Task to complete. Magically, while it is waiting the thread that runs your code won’t just sit there jammed, but will go off and find some other work to do (like drawing the UI if you had one or serving someone else’s http request if this was an API you were writing)

When the thing being awaited is done, the thread comes back and carries on where it left off, with all the variable data intact

Think of it as being a bit like reaching a point in your Xbox game where your character has to sleep for an hour. You could sit and look at the screen for an hour doing nothing, or you could save your game, go and having coffee with a friend, then come back an hour later, restore the game - the time has moved on by an hour, your character is awake and you carry on from where you were. Synchronous is watching the screen for an hour (a waste of your time). Asynchronous is having coffee

To mark that “this method supports save game” you add async to the method. It then means you can use await within which completes the magic. When a method returns a Task you can use await on it. We name those methods with Async to help indicate that we can use await on them. The task represents the ongoing job and generally isn’t that interesting to us, we just want the result. For a method that returns a Task<string> we aren’t really interested in the Task, just the string. await will wait for the Task to finish then dig the string out of it for us. In short “use await to turn a Task into an x`

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

This old post defines the difference: Asynchronous vs synchronous execution, what does it really mean?. There is a TON to unwrap, but basically:

sync methods execute when all lines of code preceding them have finished. async methods may execute without waiting for other lines to finish. await tells the following async code to wait for the preceding code, as if it were sync. Task is just a wrapper around the response to an async method, so that you don't have to write a custom "when this method returns do X with the response" closure.

(almost) ALWAYS use async Task in place of void and public async Task in place of public Foo. And while you don't have to await an async method, you should always do so if you need the result. Or use Task.Result, if you're calling an async method from a sync method.

dylanthelion
  • 1,760
  • 15
  • 23