C# 测量线程运行时间

C# 测量线程运行时间,c#,multithreading,c#-4.0,C#,Multithreading,C# 4.0,所以,我想用秒表测量“线程”运行的时间。(实际上,我想测量从创建这个线程到线程结束的时间。) 我已经把秒表放在我想要的地方了。但是我应该把秒表放在哪里呢? 谢谢。为什么不将秒表代码放在线程本身中?例如: public void Foo(IRB inR) { Stopwatch sw = new Stopwatch(); sw.Start(); System.Threading.Thread theThread = new System.Threading.Thread(

所以,我想用秒表测量“线程”运行的时间。(实际上,我想测量从创建这个线程到线程结束的时间。) 我已经把秒表放在我想要的地方了。但是我应该把秒表放在哪里呢?
谢谢。

为什么不将秒表代码放在线程本身中?例如:

public void Foo(IRB inR) {
    Stopwatch sw = new Stopwatch();
    sw.Start();

    System.Threading.Thread theThread = new System.Threading.Thread(delegate() {
            if (inR.Ready) {
                inR.ABC();
                while (!inR.Ready) { Thread.Sleep(100); }
            }
            mP.CP = false;
        });
    theThread.Name = "aaabbbccc";
    theThread.Start();
}
然后:


你能把它放在代表的末尾吗?
如果将Stopwatch对象创建为函数的局部变量,则必须将后台线程与正在运行的线程连接起来。或者,您可以在函数外部创建它,让线程在不连接的情况下运行

ThreadStart work = delegate() {
    if (inR.Ready) {
        inR.ABC();
        while (!inR.Ready) { Thread.Sleep(100); }
    }
    mP.CP = false;
};
ThreadTimer timer = new ThreadTimer(work);
Thread thread = new Thread(timer.TimeAndExecute);
thread.Start();

你能把所有秒表的东西也放在他的代表身上吗?谢谢,但不是为我工作。读卡器卡在某个地方,永远无法到达代码的最后一行。
ThreadStart work = delegate() {
    if (inR.Ready) {
        inR.ABC();
        while (!inR.Ready) { Thread.Sleep(100); }
    }
    mP.CP = false;
};
ThreadTimer timer = new ThreadTimer(work);
Thread thread = new Thread(timer.TimeAndExecute);
thread.Start();
public void ConditionPlate(IRB inR)
{

    Stopwatch sw = new Stopwatch();
    sw.Start();

    System.Threading.Thread theThread = new System.Threading.Thread(delegate()
    {
        if (inR.Ready)
        {
            inR.ABC();
            while (!inR.Ready) { Thread.Sleep(100); }
        }
        mP.CP = false;

        // ********************************
        // This will stop the stopwatch.
        // ********************************
        sw.Stop();
    });
    theThread.Name = "aaabbbccc";
    theThread.Start();

    // Wait for the thread to stop (necessary if 'sw' is created here, locally)
    theThread.Join();

    // gets time required for creation of thread to thread completion.
    var elapsed = sw.Elapsed;

}