在VS 2022社区(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);
产生的输出是:
Executing 9
Executing 10
Executing 8
我明白身份是可以改变的.但我从来没有见过id从1开始.我的朋友们也这么说.他们还补充说,VS 2019中的相同代码能够在多次try 中生成1、2和3这样的ID. 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…
我在https://developercommunity.visualstudio.com/t/Seems-like-TaskCurrentId-does-not-gener/10432547上发布了这个问题.按照建议,我开始调试代码,并发现由于此设置而创建了一些额外的任务ID.显然,当我在发布配置中运行代码时,不会创建这些额外的任务ID.但我想不出如何在VS2022中避免在调试模式下创建这些额外任务的过程. 以下是我的问题: 1.更改后的行为是预期行为吗? 2.在VS2022中,有什么简单的方法可以避免在调试模式下创建那些额外的任务?如果是这样的话,是如何做到的呢?