如何使用Name属性枚举.NET线程? [英] How to enumerate threads in .NET using the Name property?

查看:125
本文介绍了如何使用Name属性枚举.NET线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我开始两个线程是这样的:

Suppose I start two threads like this:

// Start first thread
Thread loaderThread1 = new Thread(loader.Load);
loaderThread1.Name = "Rope";
loaderThread1.Start();

// Start second thread
Thread loaderThread2 = new Thread(loader.Load);
loaderThread2.Name = "String";
loaderThread2.Start();

有什么办法,我可以用自己的名称属性枚举线程?

Is there any way I can enumerate the threads by using their Name property?

我想ablie检查是否具有特定名称线程仍在运行。

I want to be ablie to check if a thread with a specific name is still running.

创建与命名的数据集作品每个线程中,数据集的名称被用作名称为线程与数据的工作。在开始工作线程我想看看是否为同一组数据以往任何线程已经运行了。

Each thread I create works with a named set of data, the name of the data set is used as the name for the thread working with the data. Before starting a worker thread I want to see if any previous thread for the same set of data is already running.

使用时,我得到的线程 System.Diagnostics.GetCurrentProcess()。线程的类型为 ProcessThread ,而不是

The threads I get when using System.Diagnostics.GetCurrentProcess().Threads are of type ProcessThread, not Thread!

推荐答案

我怀疑你可能要忍受线程进入词典<字符串,螺纹> 为工作 - 但你为什么想要它呢?通常有线程间通信(任何锁/等待的对象)的其他方式。

I suspect you might have to put the threads into a Dictionary<string,Thread> for that to work - but why do you want it anyway? There are usually other ways of communicating between threads (any of the lock / wait objects).

要在进程级别工作(即不思<$ C $的C>主题)对象,请参见这里 - 你可以把它限制在当前的进程,但您将无法与线程交互

To work at the process level (i.e. not thinking of the Thread object), see here - you could limit it to the current process, but you won't be able to interact with the thread.

这篇关于如何使用Name属性枚举.NET线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆