.NET等同于Java线程组?

时间:2020-12-17 08:42:59

I'm trying to monitor the states of a group of threads in a Microsoft.NET application written in C#. I'd like to be able to also monitor any child threads spawned by the original threads.

我正在尝试监视用C#编写的Microsoft.NET应用程序中的一组线程的状态。我希望能够监视由原始线程生成的任何子线程。

In Java, you can assign threads to a thread group, and their children will also belong to the group. Is there an equivalent in .NET?

在Java中,您可以将线程分配给线程组,并且它们的子项也将属于该组。在.NET中有相同的东西吗?

I briefly looked at ExecutionContext and LogicalCallContext, but I can't see how to find all the threads that are in a context. Raymond Chen has an article about a Win32 API method for enumerating threads, but I hope I don't have to go that low.

我简要地看了一下ExecutionContext和LogicalCallContext,但我看不到如何查找上下文中的所有线程。 Raymond Chen有一篇关于枚举线程的Win32 API方法的文章,但我希望我没有那么低。

3 个解决方案

#1


3  

You can enumerate the threads in your process using the Threads property of System.Diagnostics.Process.

您可以使用System.Diagnostics.Process的Threads属性枚举进程中的线程。

Note however, that the objects you get here are not of the same type as those you create to start threads yourself (i.e. are not System.Threading.Thread objects).

但请注意,您在此处获得的对象与您自己创建的对象类型不同(即不是System.Threading.Thread对象)。

A concept of thread groups does not exist however, AFAIK.

然而,AFAIK不存在线程组的概念。

#2


2  

They are working on something like that in their "Task" API, which is part of Parallel Extensions.

他们正在“Task”API中进行类似的工作,这是Parallel Extensions的一部分。

#3


1  

Make it simple:

简单说明:

Create your ThreadGroup class with a method wrapping the thread creation process.

使用包装线程创建过程的方法创建ThreadGroup类。

When this method is called, it adds the created thread to a Collection and there is your group.

调用此方法时,它会将创建的线程添加到Collection中,并且存在您的组。

#1


3  

You can enumerate the threads in your process using the Threads property of System.Diagnostics.Process.

您可以使用System.Diagnostics.Process的Threads属性枚举进程中的线程。

Note however, that the objects you get here are not of the same type as those you create to start threads yourself (i.e. are not System.Threading.Thread objects).

但请注意,您在此处获得的对象与您自己创建的对象类型不同(即不是System.Threading.Thread对象)。

A concept of thread groups does not exist however, AFAIK.

然而,AFAIK不存在线程组的概念。

#2


2  

They are working on something like that in their "Task" API, which is part of Parallel Extensions.

他们正在“Task”API中进行类似的工作,这是Parallel Extensions的一部分。

#3


1  

Make it simple:

简单说明:

Create your ThreadGroup class with a method wrapping the thread creation process.

使用包装线程创建过程的方法创建ThreadGroup类。

When this method is called, it adds the created thread to a Collection and there is your group.

调用此方法时,它会将创建的线程添加到Collection中,并且存在您的组。