在 VS 2022 Community(版本 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 可以更改。但我从来没有看到id从1开始。我的朋友也报告了同样的情况。他们还补充说,VS 2019 中的相同代码能够在多次尝试中生成 1,2 和 3 等 id。 以下是总结的观察结果:
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中调试模式下创建这些附加任务的过程?如果是的话,怎么办?