Search code examples
c#.netvisual-studiovisual-studio-2019visual-studio-2022

VS2022 versus VS2019: How/Why are the additional tasks being created?


When Executing the following code in VS 2022 Community(version 17.8.3):

using static System.Console;

Action action1 = new(() => WriteLine($"Executing {Task.CurrentId}"));
Action action2 = new(() => WriteLine($"Executing {Task.CurrentId}"));
Action action3 = new(() => WriteLine($"Executing {Task.CurrentId}"));
Parallel.Invoke(action1, action2, action3);

are producing output like:

Executing 9
Executing 10
Executing 8

I understand that the id can change. But I never saw the id starts from 1. My friends report the same. They also add that the same code in VS 2019 was able to produce ids like 1,2, and 3 on multiple try. And here are the summarised observation:

1.VS2019-debug and release- You can see IDs 1,2,3 etc
2.VS 2022 release- Same observation.
3.VS2022-Debug- IDs start appearing as 8,9,10; 9,10,11 or 10,11,12 ,and so on…

I posted the question on https://developercommunity.visualstudio.com/t/Seems-like-TaskCurrentId-does-not-gener/10432547. Following the suggestion, I started debugging the code and found that some additional task ids were created due to this setting. Obviously, those additional task ids were not created when I ran the code in release configuration. But I could not figure out how to avoid the creational process of those additional tasks in debug mode in VS2022. Here are my questions: 1.Is the changed behavior an expected behavior? 2. is there any simple way to avoid the creational process of those additional tasks in debug mode in VS2022? If so, how?


Solution

  • This is caused by hot reload functionality as far as I can see.

    If you place breakpoint at the start of the app and will go to the Parallel Stacks window (Debug -> Windows -> Parallel Stacks, switch to task view) you can see something like:

    enter image description here

    With some stackframes referring to stuff from Microsoft.Extensions.HotReload namespace.

    You can try disabling the hot reload: go to the project properties -> Debug -> Open debug launch profiles UI -> uncheck Enable Hot Reload option:

    enter image description here

    enter image description here

    This results in "correct" task ids for me.