我想计算每个线程的处理时间。
我该怎么做。?
假设我的100个线程正在同时执行相同的方法work(),那么如果我输入以下代码,可以帮助我得到想要的结果

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);

最佳答案

GetThreadTimes()提供此信息。但是,由于它需要线程的句柄,因此很难使用。.NET框架不允许您这样做。一个简单的解决方案是在线程函数内部启动一个秒表,并在其完成时使用Stop()。

关于c# - 每个线程的处理时间?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3165440/

10-11 18:21