相关疑难解决方法(0)

从ProcessThreadCollection获取名称的运行线程

在搜索Stack Overflow问题和一些谷歌搜索后,我仍然没有得到它.

我知道您可以使用"Thread.isAlive()"方法检查单个线程是否正在运行,但是我想检查特定的"FooThread"是否仍然在当前进程的所有正在运行的线程之间运行,如果没有,调用启动它的方法.

//somewhere in the code, on another Project/DLL inside solution
private void FooThreadCaller()
{
    Action act = () => fooFunction();
    Thread t = new Thread(new ThreadStart(act));
    t.Name = "FooThread";
    t.Start();
}
//...
Process proc = System.Diagnostics.Process.GetCurrentProcess();
ProcessThreadCollection threads = proc.Threads;
bool ThreadExists = false
foreach (ProcessThread item in threads)
{
    // if item.Name == "FooThread", then ThreadExists = true...
    // so, if !ThreadExists then call FooThreadCaller() and so on.
}
//...
Run Code Online (Sandbox Code Playgroud)

由于ProcessThread类没有"Name"属性(如System.Threading.Thread那样)但只有ID,而我只知道线程名称("FooThread")而不是ID,我怎么能检查"FooThread"正在运行/活着?

先感谢您

.net c# multithreading process

10
推荐指数
1
解决办法
1万
查看次数

如何在c#中测量线程的时间?

我想测量一下C#例程所需的时间.因为还有很多其他线程我只想计算这一个线程的时间.在Java中我可以使用getCurrentThreadCpuTime.

我该怎么做?

c# performance multithreading

8
推荐指数
1
解决办法
827
查看次数

标签 统计

c# ×2

multithreading ×2

.net ×1

performance ×1

process ×1