为什么GetThreadTimes会返回

我正在尝试测量线程在进度报告中花费的时间,但是我从GetThreadTimes系统调用得到了非常奇怪的结果。 鉴于以下程序(在VS 2013中编译,以.NET 4.5为目标):

using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; namespace ThreadTimingTest { class Program { static Stopwatch _wallClockTimer; static System.Timers.Timer _timer = new System.Timers.Timer(); private static Thread _thread; private static IntPtr _threadHandle; static void Main(string[] args) { _timer = new System.Timers.Timer(); _timer.Elapsed += (s, e) => { System.Runtime.InteropServices.ComTypes.FILETIME start, end, rawKernelTime, rawUserTime; GetThreadTimes(_threadHandle, out start, out end, out rawKernelTime, out rawUserTime); //ref: http://stackoverflow.com/a/6083846 ulong uLow = (ulong)rawKernelTime.dwLowDateTime; ulong uHigh = (uint)rawKernelTime.dwHighDateTime; uHigh = uHigh << 32; long kernelTime = (long)(uHigh | uLow); uLow = (ulong)rawUserTime.dwLowDateTime; uHigh = (uint)rawUserTime.dwHighDateTime; uHigh = uHigh < { _threadHandle = GetCurrentThread(); Stopwatch sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds < 3000) { int i = 1 + 2; } //do busy-work for 3 seconds sw.Stop(); } ); _timer.Start(); _thread.Start();a _wallClockTimer.Start(); _thread.Join(); } [DllImport("kernel32.dll", SetLastError = true)] static extern bool GetThreadTimes(IntPtr hThread, out System.Runtime.InteropServices.ComTypes.FILETIME lpCreationTime, out System.Runtime.InteropServices.ComTypes.FILETIME lpExitTime, out System.Runtime.InteropServices.ComTypes.FILETIME lpKernelTime, out System.Runtime.InteropServices.ComTypes.FILETIME lpUserTime); [DllImport("kernel32.dll")] private static extern IntPtr GetCurrentThread(); } } 

我得到以下输出:

 Starting... Kernel time: 0 User time: 0 Combined raw execution time: 0 Elapsed thread time: 0 milliseconds Wall Clock Time: 1036 milliseconds Kernel time: 0 User time: 0 Combined raw execution time: 0 Elapsed thread time: 0 milliseconds Wall Clock Time: 2036 milliseconds The thread '' (0x191c) has exited with code 0 (0x0). Ended. 

我希望GetThreadTimes报告线程时间以外的其他内容:为什么报告零?

根据Hans提供的链接为代码制作几个简单的mod后,会显示有效时间。

添加一些互操作声明:

 [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, IntPtr hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle, uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwOptions); [Flags] public enum DuplicateOptions : uint { DUPLICATE_CLOSE_SOURCE = (0x00000001), // Closes the source handle. This occurs regardless of any error status returned. DUPLICATE_SAME_ACCESS = (0x00000002), // Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle. } [DllImport("kernel32.dll")] static extern IntPtr GetCurrentProcess(); 

然后修改句柄的分配方式:

 //_threadHandle = GetCurrentThread(); <-- previous assignment IntPtr processHandle = GetCurrentProcess(); bool result = DuplicateHandle(processHandle, GetCurrentThread(), processHandle, out _threadHandle, 0, false, (uint) DuplicateOptions.DUPLICATE_SAME_ACCESS); 

产生以下结果:

 Starting... Kernel time: 0 User time: 10000000 Combined raw execution time: 10000000 Elapsed thread time: 1000 milliseconds Wall Clock Time: 1006 milliseconds Kernel time: 0 User time: 20000000 Combined raw execution time: 20000000 Elapsed thread time: 2000 milliseconds Wall Clock Time: 2004 milliseconds Kernel time: 0 User time: 30000000 Combined raw execution time: 30000000 Ended. Elapsed thread time: 3000 milliseconds Wall Clock Time: 3045 milliseconds 

编辑:

最近,为处理为给定系统创建的太multithreading已经付出了很多努力。 假设你有一个四核处理器,并且有20多个线程都想运行。 线程在启动,内核管理,内存(它们有自己的堆栈)等方面的成本相当高。系统实际上可能比线程数减少时更慢(杂乱的上下文和调度)。 所以在.NET中,创建了像TPL这样的库(允许开发人员管理任务,而不是线程)。 这允许CLR平衡适当的线程数到目标系统。 但在您的情况下(显式创建托管线程),与本机线程几乎总是存在1对1的关系。

希望这可以帮助。