1

I have this method:

public async Task DoSomething()
{
    string token = string.Empty;

    await Task.Run(() =>
    {
        using (var sc = _dbManager.CreateSession())
        {
            // somework...
        }
    });

    using (var resp = await client.PostAsync(authenticationEndpoint,
        new FormUrlEncodedContent(authenticateRequest), stoppingToken))
    {
        resp.EnsureSuccessStatusCode();
        string stringResponse = await resp.Content.ReadAsStringAsync(stoppingToken);
        var response = JsonConvert.DeserializeObject<Response>(stringResponse);
        token = response.AccessToken;
    }
}

Now I do not understand why I need to use await Task.Run? It's confusing for me to understand logic behind this, as I'm awaiting the task to finish anyway, so why should I put it in the Task in the first place?

My lead told me that this was the way to go, and also explained to me that if I don't put that in Task, the code will be executed again from the top, after await client.PostAsync.

Stefan Wuebbe
  • 2,109
  • 5
  • 17
  • 28
chessGeek
  • 11
  • 3
  • 1
    It depends - is it desktop app or ASP NET webservice? Does database have asynchronous API ? What do you use to connect to database? – Michał Turczyn Dec 29 '22 at 09:23
  • _"Now I do not understand why I need to use await Task.Run?"_ Because you can `await` only `async` code and without your `Task.Run()` it would be rather "sync" – Stefan Wuebbe Dec 29 '22 at 09:27
  • Sorry for missing that. It is a background worker in .NET Core. No, the database does not have asynchronous API, I am using Dapper. – chessGeek Dec 29 '22 at 09:28
  • @StefanWuebbe but then should I put http request also in task? Also, I have some more code under httpclient, where I do not use Task.Run. – chessGeek Dec 29 '22 at 09:29
  • 2
    I'd say, the point of async/await is to keep the UI thread unblocked, so putting (any) possibly lengthy synchronous code into a `Task.Run()` can improve the UI reactability – Stefan Wuebbe Dec 29 '22 at 09:35
  • On the other hand I'd also say, with async/await you'd normally not need a `BackgroundWorker` anymore – Stefan Wuebbe Dec 29 '22 at 09:39
  • "if I don't put that in Task, the code will be executed again from the top, after await client.PostAsync" - how is that? – Evk Dec 29 '22 at 09:44
  • 1
    Related: [await Task.Run vs await](https://stackoverflow.com/questions/38739403/await-task-run-vs-await). This is also an interesting reading: [Should I expose asynchronous wrappers for synchronous methods?](https://devblogs.microsoft.com/pfxteam/should-i-expose-asynchronous-wrappers-for-synchronous-methods/) – Theodor Zoulias Dec 29 '22 at 09:53
  • @StefanWuebbe in this case there is no UI. Evk that is what I was told, it is the reason why I posted question. – chessGeek Dec 29 '22 at 10:07
  • From given info it seems to me there is no reason to do that in your case. – Evk Dec 29 '22 at 10:09
  • By "background worker", do you mean `BackgroundWorker`, or do you mean `BackgroundService`, or something else? Is this code run in a UI app or web app or something else? – Stephen Cleary Dec 29 '22 at 13:20
  • It is BackgroundService. It is not running in UI app or web. It is windows service – chessGeek Dec 29 '22 at 13:38

0 Answers0