Task.Run vs. direct async call for starting long-running async methods

ChaseMedallion

Several times, I have found myself writing long-running async methods for things like polling loops. These methods might look something like this:

private async Task PollLoop()
{
    while (this.KeepPolling)
    {
        var response = await someHttpClient.GetAsync(...).ConfigureAwait(false);
        var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

        // do something with content

        await Task.Delay(timeBetweenPolls).ConfigureAwait(false);
    }
}

The goal of using async for this purpose is that we don't need a dedicated polling thread and yet the logic is (to me) easier to understand than using something like a timer directly (also, no need to worry about reentrance).

My question is, what is the preferred method for launching such a loop from a synchronous context? I can think of at least 2 approaches:

var pollingTask = Task.Run(async () => await this.PollLoop());

// or

var pollingTask = this.PollLoop();

In either case, I can respond to exceptions using ContinueWith(). My main understanding of the difference between these two methods is that the first will initially start looping on a thread-pool thread, whereas the second will run on the current thread until the first await. Is this true? Are there other things to consider or better approaches to try?

usr

My main understanding of the difference between these two methods is that the first will initially start looping on a thread-pool thread, whereas the second will run on the current thread until the first await. Is this true?

Yes. An async method returns its task to its caller on the first await of an awaitable that is not already completed.

By convention most async methods return very quickly. Yours does as well because await someHttpClient.GetAsync will be reached very quickly.

There is no point in moving the beginning of this async method onto the thread-pool. It adds overhead and saves almost no latency. It certainly does not help throughput or scaling behavior.

Using an async lambda here (Task.Run(async () => await this.PollLoop())) is especially useless. It just wraps the task returned by PollLoop with another layer of tasks. it would be better to say Task.Run(() => this.PollLoop()).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Recursively call async method vs long running task when receiving data from TcpClient

From Dev

Recursively call async method vs long running task when receiving data from TcpClient

From Dev

async await calling long running sync AND async methods

From Dev

What difference does it make - running an 'async' action delegate with a Task.Run (vs default action delegate)?

From Dev

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

From Dev

Async Task not running in background?

From Dev

cancel async task if running

From Dev

Async Task not running in background?

From Dev

Fire and forget async Task vs Task.Run

From Dev

Fire and forget async Task vs Task.Run

From Dev

Windows 10 UWP app async/await usage on long running task

From Dev

Running async methods in parallel

From Dev

Call Async task repeatedly

From Dev

Async/Await VS Task.Run : When to use ? How to use?

From Dev

Async/await vs Task.Run in C#

From Dev

Usage of async methods in isolated task

From Dev

How to async this long running method?

From Dev

Running an async background task in Tornado

From Dev

how to stop a running async task?

From Dev

Running async task after activitiy

From Dev

Async Task with sync method call

From Dev

How to call the async Task Function?

From Dev

Running a long-running parallel task in the background, while allowing small async tasks to update the foreground

From Dev

Async Task.Run Not Working

From Dev

Run async task periodically in android

From Dev

Async Task.Run Not Working

From Dev

IHttpActionResult vs async Task<IHttpActionResult>

From Dev

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

From Dev

Using async to run multiple methods

Related Related

  1. 1

    Recursively call async method vs long running task when receiving data from TcpClient

  2. 2

    Recursively call async method vs long running task when receiving data from TcpClient

  3. 3

    async await calling long running sync AND async methods

  4. 4

    What difference does it make - running an 'async' action delegate with a Task.Run (vs default action delegate)?

  5. 5

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

  6. 6

    Async Task not running in background?

  7. 7

    cancel async task if running

  8. 8

    Async Task not running in background?

  9. 9

    Fire and forget async Task vs Task.Run

  10. 10

    Fire and forget async Task vs Task.Run

  11. 11

    Windows 10 UWP app async/await usage on long running task

  12. 12

    Running async methods in parallel

  13. 13

    Call Async task repeatedly

  14. 14

    Async/Await VS Task.Run : When to use ? How to use?

  15. 15

    Async/await vs Task.Run in C#

  16. 16

    Usage of async methods in isolated task

  17. 17

    How to async this long running method?

  18. 18

    Running an async background task in Tornado

  19. 19

    how to stop a running async task?

  20. 20

    Running async task after activitiy

  21. 21

    Async Task with sync method call

  22. 22

    How to call the async Task Function?

  23. 23

    Running a long-running parallel task in the background, while allowing small async tasks to update the foreground

  24. 24

    Async Task.Run Not Working

  25. 25

    Run async task periodically in android

  26. 26

    Async Task.Run Not Working

  27. 27

    IHttpActionResult vs async Task<IHttpActionResult>

  28. 28

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

  29. 29

    Using async to run multiple methods

HotTag

Archive