代码:
public class Test
{
public static void Main()
{
int threadCount = ThreadPool.ThreadCount;
Console.WriteLine($"thread count : {threadCount}");
ThreadPool.SetMinThreads(1, 2);
Console.WriteLine(ThreadPool.SetMaxThreads(1, 2));
List<Task> tlist = new();
for (int i = 0; i < 3; i++)
{
Console.WriteLine($"repeat {i}");
tlist.Add(Task.Run(() =>
{
threadCount = ThreadPool.ThreadCount;
Console.WriteLine($"thread count : {threadCount}");
return FuncAsync();
}));
Console.WriteLine($"repeat end {i}");
}
Task.WaitAll(tlist.ToArray());
}
public static async Task FuncAsync()
{
Console.WriteLine("FuncAsync");
}
}
这段代码只有在"Run"with"Release"Build的情况下才能运行.
Debugging | Running | |
---|---|---|
Debug build | work | unwork |
Release build | work | work |
首先,对不起,我的台球英语能力…
我想知道当线程池的最大容量为1时,异步方法是如何工作的. 但我遇到了一些意想不到的错误.
When I try to run with debug build, thread pool start with already filling with 2 thread.
So, there's no affordable thread in ThreadPool
, then it doesn't work. Just stop infinitely ...
但这种情况从一开始就不会发生,
- 使用调试版本进行调试
- 使用发布版本进行调试
- 使用发布版本运行
有人知道为什么会出现这种情况吗?