当这个程序响应缓慢时,它在做什么?

What is this program doing when it responds slowly?

我在 Windows 8.1 x64 上使用 C# .NET 4.0 应用程序,它需要很长时间(18 秒)才能执行某些操作。我想尝试诊断为什么会这样。 Process Monitor 跟踪显示:

╔═══════════════╦════════════════════════════════════════╗
║ Relative Time ║               Operation                ║
╠═══════════════╬════════════════════════════════════════╣
║ 00:02.000000  ║ Thread Create                          ║
║ 00:02.000100  ║ Thread Create                          ║
║ 00:04.000000  ║ Thread Create                          ║
║ 00:04.000100  ║ Thread Create                          ║
║ 00:04.000200  ║ Thread Create                          ║
║ 00:04.000300  ║ Thread Exit                            ║
║ 00:04.000400  ║ Thread Create                          ║
║ 00:04.000500  ║ TCP Disconnect                         ║
║ 00:04.000600  ║ Thread Exit                            ║
║ 00:06.000000  ║ Thread Create                          ║
║ 00:06.000100  ║ Thread Create                          ║
║ 00:06.000200  ║ Thread Create                          ║
║ 00:06.000300  ║ Thread Create                          ║
║ 00:06.000400  ║ Thread Create                          ║
║ 00:07.000000  ║ Thread Exit                            ║
║ 00:10.000000  ║ Thread Create                          ║
║ 00:11.500000  ║ CreateFile                             ║
║               ║ // read the file contents              ║
║ 00:11.540000  ║ CloseFile                              ║
║ 00:11.541000  ║ CreateFile                             ║
║               ║ // read the file contents              ║
║ 00:11.600000  ║ CloseFile                              ║
║ 00:12.100000  ║ Thread Exit                            ║
║ 00:19.000000  ║ Thread Exit                            ║
║ 00:20.000000  ║ Thread Exit                            ║
║               ║ // from here on things respond quickly ║
╚═══════════════╩════════════════════════════════════════╝

有几个很大的差距,我想更好地理解程序在做什么。假设我可以获得符号(但可能不是源代码),我能做些什么来更好地理解发生了什么吗?我愿意使用 WinDbg 或 Windows 性能监视器。我只是想了解从哪里开始使用这些工具来缩小这些特定差距的方向。

更好的选择是 运行 通过分析器简单地编写您的程序。 Visual Studio 有一个很好的,它在 "Analyze" 菜单下(至少在 VS 2012 中)。

使用探查器可以准确显示哪些代码占用了您所有的时间,具体到各个方法调用。以你的information/tools,获取这些信息将非常困难。