【发布时间】:2014-02-20 21:53:23
【问题描述】:
我有这段代码生成 6 个线程并在每个线程上同时执行 GetAverage 方法。
var threadFinishEvents = new List<EventWaitHandle>();
//StopWatch sw = new StopWatch();
for (int i = 0; i < 6; i++)
{
var threadFinish = new EventWaitHandle(false, EventResetMode.ManualReset);
threadFinishEvents.Add(threadFinish);
var thread = new Thread(delegate()
{
GetAverage(userName);
threadFinish.Set();
//sw.Stop();
});
//sw.Start();
thread.Start();
}
Mutex.WaitAll(threadFinishEvents.ToArray(), threadTimeout);
long timeElapsed = sw.TimeElapsed();
我想记录每个线程执行的时间并将其记录到数据库中。我在谷歌上阅读了很多建议使用Stopwatch 的帖子。我尝试将Stopwatch 代码(我已注释掉)添加到 sn-p 但不确定这是否正确。有人可以帮帮我吗?
【问题讨论】:
标签: c# .net multithreading stopwatch