Is Async/Await using Task.Run starting a new thread asynchronously?

后端 未结 3 1494
耶瑟儿~
耶瑟儿~ 2020-12-02 15:41

I have read a lot of articles and still cant get understand this part.

Consider this code :

    private async void button1_Click(object sender, Event         


        
3条回答
  •  离开以前
    2020-12-02 16:13

    1. The purpose of creating Async methods is so you can Await them later. Kind of like "I'm going to put this water on to boil, finish prepping the rest of my soup ingredients, and then come back to the pot and wait for the water to finish boiling so I can make dinner." You start the water boiling, which it does asynchronously while you do other things, but eventually you have to stop and wait for it. If what you want is to "fire-and-forget" then Async and Await are not necessary.

    Simplest way to do a fire and forget method in C#?

    1. Starting a new task queues that task for execution on a threadpool thread. Threads execute in the context of the process (eg. the executable that runs your application). If this is a web application running under IIS, then that thread is created in the context of the IIS worker process. That thread executes separately from the main execution thread, so it goes off and does its thing regardless of what your main execution thread is doing, and at the same time, your main execution thread moves on with its own work.

提交回复
热议问题