Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我试图找出我的应用程序正在运行多少线程,以便制作性能的实时图表。您能指出我正确的方向或提供代码示例吗?谢谢!
您可以查看当前进程的Threads属性:
System.Diagnostics.Process.GetCurrentProcess().Threads
当然,这也包括非托管线程。
如果您只想查看托管线程:
当您从 Visual Studio 调试应用程序时,您还可以查看ThreadsandParallel Stacks窗口,该窗口位于菜单下Debug -> Windows
Threads
Parallel Stacks
Debug -> Windows
您可以在下图Thread-Count中的顶部Threads窗口中看到(标记为绿色)
Thread-Count
private static int s_threadCount; private static void MyMethod() { Interlocked.Increment(ref s_threadCount); try { ... } finally { Interlocked.Decrement(ref s_threadCount); } }