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

后端 未结 3 1503
耶瑟儿~
耶瑟儿~ 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:07

    The type Task requires you to return a TResult from your task. If you don't have anything to return, you can use Task instead (which, incidentally, is the base class of Task).

    But keep in mind that a task is not a thread. A task is a job to be done, while a thread is a worker. As your program runs, jobs and workers become available and unavailable. Behind the scenes, the library will assign your jobs to available workers and, because creating new workers is a costly operation, it will typically prefer to reuse the existing ones, through a thread pool.

提交回复
热议问题