2010-07-02 59 views
2

我想計算每個線程的處理時間。 我該怎麼做。? 想我的100個線程都在執行同樣的方法工作()同時,那麼如果我把下面的代碼幫我把我所追求每個線程的處理時間?

Process thisProc = Process.GetCurrentProcess(); 
string procName = thisProc.ProcessName; 
DateTime started = thisProc.StartTime; 

int memory = thisProc.VirtualMemorySize; 
int priMemory = thisProc.PrivateMemorySize; 
int physMemory = thisProc.WorkingSet; 

ProcessPriorityClass priClass = thisProc.PriorityClass; 
TimeSpan cpuTime = thisProc.TotalProcessorTime; 

Console.WriteLine(" started: {0}", started.ToString()); 
Console.WriteLine(" CPU time: {0}", cpuTime.ToString()); 

Console.WriteLine(" Virtual Memory: {0}", memory + " ; Private Memory: " + priMemory + " ; Physical Memory: " + physMemory); 

回答

3

GetThreadTimes()提供了這個信息。但是很難使用,因爲它需要線程的句柄,.NET框架不允許你這樣做。簡單的解決方案是在線程函數內啓動Stopwatch,並在完成時停止Stop()。

+0

好的。 非常感謝 – phen 2010-07-02 12:22:26