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

syncis

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

Consider this code :

    private async void button1_Click(object sender, EventArgs e)
    {
        await Dosomething();
    }

    private async Task<string> Dosomething()
    {
        await Task.Run((() => "Do Work"));
        return "I am done";
    }

First question:

When I click the button, it will Call DoSomething and await a Task that creates a Thread from the threadpool by calling Task.Run ( if I am not mistaken ) and all of this runs asynchronously. So I achieved creating a thread that does my work but doing it asynchronously? But consider that I don't need any result back, i just want the work to be done without getting any result back, is there really a need to use async/await , and if so, how?

Second question:

When running a thread asynchronously, how does that work? Is it running on the main UI but on a separate thread or is it running on a separate thread and separate is asynchronously inside that method?

DVK
  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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does starting a new Task get invoked on the UI Thread

From Dev

Run grunt task asynchronously before another task

From Dev

How to run a window into a new task and keep its main thread running

From Dev

How to run a window into a new task and keep its main thread running

From Dev

Creating and starting a task on the UI thread

From Dev

Starting a new thread on Exception using Uncaught Exceptional Handler

From Dev

Starting activity using thread

From Dev

Task.Factory.New vs Task.Run vs async/await, best way to not block UI thread

From Dev

Starting new Thread freezes UI

From Dev

Unable to run batch starting Excel as scheduled task

From Dev

Using Thread.new in Rails to run destroy_all or Thread.new to wrap model methods in transaction

From Dev

Check if task is already running before starting new

From Dev

Using asyncawait module with function

From Dev

Await new Task<T>( ... ) : Task does not run?

From Dev

Why Task canceled run again with new Task?

From Dev

IntentService starting new service - on which thread?

From Dev

Will creating a new task generate a background thread or a thread pool thread

From Dev

using thread to run a method

From Dev

eclipse starting new instance of java to run the program

From Dev

How to run a task on a specific thread which is active?

From Dev

run a task on ui thread from fragment

From Dev

How to run a task on a specific thread which is active?

From Dev

Is new Task always executed on ThreadPool thread?

From Dev

starts new thread inside Task.ContinueWith

From Dev

Task.WhenAll() - does it create a new thread?

From Dev

Cant install asyncawait using npm

From Dev

Starting a member function in a thread using class constructor

From Dev

Using std::async() & std::move() to pass data to another thread asynchronously

From Dev

Get a function result asynchronously in Delphi using Omni Thread Library

Related Related

  1. 1

    Why does starting a new Task get invoked on the UI Thread

  2. 2

    Run grunt task asynchronously before another task

  3. 3

    How to run a window into a new task and keep its main thread running

  4. 4

    How to run a window into a new task and keep its main thread running

  5. 5

    Creating and starting a task on the UI thread

  6. 6

    Starting a new thread on Exception using Uncaught Exceptional Handler

  7. 7

    Starting activity using thread

  8. 8

    Task.Factory.New vs Task.Run vs async/await, best way to not block UI thread

  9. 9

    Starting new Thread freezes UI

  10. 10

    Unable to run batch starting Excel as scheduled task

  11. 11

    Using Thread.new in Rails to run destroy_all or Thread.new to wrap model methods in transaction

  12. 12

    Check if task is already running before starting new

  13. 13

    Using asyncawait module with function

  14. 14

    Await new Task<T>( ... ) : Task does not run?

  15. 15

    Why Task canceled run again with new Task?

  16. 16

    IntentService starting new service - on which thread?

  17. 17

    Will creating a new task generate a background thread or a thread pool thread

  18. 18

    using thread to run a method

  19. 19

    eclipse starting new instance of java to run the program

  20. 20

    How to run a task on a specific thread which is active?

  21. 21

    run a task on ui thread from fragment

  22. 22

    How to run a task on a specific thread which is active?

  23. 23

    Is new Task always executed on ThreadPool thread?

  24. 24

    starts new thread inside Task.ContinueWith

  25. 25

    Task.WhenAll() - does it create a new thread?

  26. 26

    Cant install asyncawait using npm

  27. 27

    Starting a member function in a thread using class constructor

  28. 28

    Using std::async() & std::move() to pass data to another thread asynchronously

  29. 29

    Get a function result asynchronously in Delphi using Omni Thread Library

HotTag

Archive