あなたは時間を待っているシステム機能GetThreadTimes http://msdn.microsoft.com/en-us/library/ms683237(v=vs.85).aspx
を使用して、ここでhttp://www.codeproject.com/KB/dotnet/ExecutionStopwatch.aspx 説明されたアプローチを使用することができますが、合計時間と実行時間との差です。
追加: 私はパフォーマンスを測定するための使い捨てのクラスを使用したい - それが(ちょうど例えばハードコードコンソールの使用状況)コードのクリーンを保つ:
public class ThreadPerformance : IDisposable
{
private Stopwatch _totalStopwatch = new Stopwatch();
private ExecutionStopwatch _executionStopwatch = new ExecutionStopwatch();
public static ThreadPerformance Measure()
{
return new ThreadPerformance();
}
private ThreadPerformance()
{
_totalStopwatch.Start();
_executionStopwatch.Start();
}
public void Dispose()
{
_executionStopwatch.Stop();
_totalStopwatch.Stop();
Console.WriteLine("Performance mesurement for thread {0}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("Waiting: {0}", _totalStopwatch.Elapsed - _executionStopwatch.Elapsed);
Console.WriteLine("CPU usage: {0}", _executionStopwatch.Elapsed);
}
}
使い方は非常に簡単です:
static void ThreadProc()
{
using (ThreadPerformance.Measure())
{
// do some calculations and waitings here
}
}
ありがとうございましたlazyberezovsky。私は同じ行で考えていた。このソリューションが異なるOSプラットフォーム/ .NETバージョン/ハードウェアプラットフォームで問題なく動作するかどうかはわかりません。 Microsoftのドキュメントでは、.NET管理スレッドが常に同じアンマネージドスレッドにマップされるという保証はありません。 GetCurrentThread()が2つの別々の呼び出しで異なるスレッドハンドルを返す場合はどうなりますか? – Nitin