System.Threading.Thread 和 System.Diagnostics.ProcessThread 之间的关联是什么?

huangapple go评论69阅读模式
英文:

What's the link between System.Threading.Thread and System.Diagnostics.ProcessThread

问题

这个先前的问题 解释了如何为线程命名。

然而,问题在于它提到了 ProcessThread,我想知道如何将这样的 ProcessThread 与首次创建的 System.Threading.Thread 关联起来。

英文:

This previous question explains what to in order to give a name to a thread.

The problem, however, is that this speaks about ProcessThread, and I would like to know how to link such a ProcessThread with the firstly created System.Threading.Thread.

答案1

得分: 1

在我的应用程序中,线程在各个地方创建并启动。甚至会出现线程多次创建和启动的情况。我想通过询问“告诉我哪些线程当前正在运行,我将在此处查找要启动的线程,但如果线程已经在运行,我就不会这样做”,来避免这种情况。

正如@Ralf建议的,最简单的方法是完全自己控制。而不是问“哪些线程当前正在运行”,您可以在创建新线程时填充一个数据结构,以便以后查询。从本质上讲,您的问题是一个依赖管理的问题。

值得指出的是,您的应用程序带有一些传统。如评论中所提到的,直接使用低级线程会带来一系列问题。为了使应用程序跟上当前的发展,您可以重构您的应用程序,使用 Task 而不是 Thread

相同的状态管理适用。您可以使用数据结构(或字典)来跟踪您的任务,然后通过 task.TaskStatus 监视它们的进度。

英文:

> in my application, threads get created and started all over the place. It goes even that far, that threads get created and started up multiple times. I would like to avoid this, simply by asking "Tell me which threads are currently running, I will search for the thread I want to start up here, but when the thread is already running, I won't do that.".

As suggested by @Ralf, the path of least resistance will be to take full control yourself. Instead of asking "which threads are currently running", you could hydrate a data structure with the new thread(s) whenever you create it. Allowing you to query it later. In essence your problem is one of dependency management.

It's worth pointing out that your application brings it legacy. As mentioned in the comments, working directly with low level threads brings it own slew of problems. To bring the application into the current decade, you could refactor your app to use Task instead of Thread.

The same state management applies. You can keep track of your tasks using a data structure (or dictionary) and then monitor their progress through task.TaskStatus.

huangapple
  • 本文由 发表于 2023年7月13日 16:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定