简体   繁体   English

如何确定Thread对象是否是.NET中的当前线程?

[英]How to determine whether a Thread object is the current thread in .NET?

Suppose I have a reference to a Thread object, such as: 假设我有一个Thread对象的引用,例如:

System.Threading.Thread thread;

If I want to determine whether the current thread is represented by this object, should the following code be used? 如果我想确定当前线程是否由此对象表示,是否应使用以下代码?

bool currentThread = (thread.ManagedThreadId == Thread.CurrentThread.ManagedThreadId);

I'm pretty sure this will work fine, but it seems strange that there wouldn't be a more direct method for checking this. 我很确定这样可以正常工作,但似乎很奇怪,没有更直接的方法来检查它。 Is there one? 有吗?

只需使用Thread.CurrentThread

bool currentThread = (thread == Thread.CurrentThread);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM