0

I have an async function that doesn't throw anything:

public async Task SomeLongComputations()
{
    // do some stuff that takes a lot of time
}

that I just want it to run in the background, without intervening the main thread:

public void SomeFunctionInMainThread()
{
    _ = SomeLongComputations();
}

But when I debugged the code in Visual Studio and set a breakpoint inside SomeLongComputations, it still shows that it is ran on main thread (thread id == 1).


Some additional context, not sure if it is relevant:

  • I am developing an extension for Visual Studio
  • The threads are created inside an event handler for ITextBuffer.Changed and IWpfTextView.LayoutChanged
  • In the debug window, I can still see other threads
Chan Kha Vu
  • 9,834
  • 6
  • 32
  • 64
  • 7
    Slapping `async Task` in front of your method doesn't actually run any code in a different thread. Where is the rest of your code? – Idle_Mind Jul 16 '20 at 22:34
  • 1
    You should expand `// do some stuff that takes a lot of time` cause just marking method `async` will not make it running in another thread. – Guru Stron Jul 16 '20 at 22:34
  • Oh, so `_ = Something` does not execute the task in new thread? – Chan Kha Vu Jul 16 '20 at 22:36
  • 1
    @FalconUA depends on what is in `Something`. – Guru Stron Jul 16 '20 at 22:36
  • @GuruStron there are some `await` stuffs in `SomeLongComputations`, but I want the whole function `SomeLongComputations` to run in different thread. How can I achieve that? Or I am misunderstanding what `Task` are in C#? – Chan Kha Vu Jul 16 '20 at 22:38
  • 1
    Please review [MCVE] guidance on posting code. There is no indication of any async processing in code shown. You've also hopefully read https://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread - if not - reading that will help with the [edit]... – Alexei Levenkov Jul 16 '20 at 22:43
  • 1
    Read the blog from Stephen Cleary and start at [Async and Await](https://blog.stephencleary.com/2012/02/async-and-await.html) – Sir Rufo Jul 16 '20 at 22:54
  • @AlexeiLevenkov Thanks, but this post is mainly to clear my misunderstanding of how tasks works. Thanks everyone to point out to my misunderstandings. – Chan Kha Vu Jul 16 '20 at 23:06

2 Answers2

2

Just adding async will not make the task run on another thread. You can use Task.Run and call the method to run it in background. See link here https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=netcore-3.1

2

I just want it to run in the background, without intervening the main thread:

That'd look more like:

public async void SomeFunctionInMainThread()
{
    await SomeLongComputations();
}

public Task SomeLongComputations()
{
    return Task.Run(() =>
    {
        // do some stuff that takes a lot of time
    });
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • 3
    Please, no ... read [Task.Run Etiquette Examples](https://blog.stephencleary.com/2013/11/taskrun-etiquette-examples-even-in.html) – Sir Rufo Jul 16 '20 at 22:52