我如何获得_real_线程ID在CLR"友好和QUOT;办法? [英] How do I get the _real_ thread id in a CLR "friendly" way?

查看:206
本文介绍了我如何获得_real_线程ID在CLR"友好和QUOT;办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个练习,我在写一些代码来显示O / S流程和O / S线程的过程中(如Sysinternals的进程资源管理器)。



我发现.NET的ManagedThreadId(S)不是O / S线程ID。有点看完后我遇到AppDomain.GetCurrentThreadId来了()。不幸的是,该功能被标记为过时(这可能意味着在未来的不可用)。一个解决方案,我发现是使用InteropServices直接调用Win32 GetCurrentThreadId。 。我很好与该但是,它反觉得到.NET的哲学



我的问题是:是否有一个CLR获得的真实身份的友好的方式当前线程?



有关参考,在这里是代码显示我到目前为止已经试过片段。 // 1和// 2显示了正确的线程ID,// 3 // 4 ...是试图获得一个CLR友好的方式相同的方式(但他们没有工作。)



感谢你的帮助,



约翰。

 函数[DllImport(KERNEL32.DLL)] 
静态外部INT GetCurrentThreadId();

静态无效的主要(字串[] args)
{
// AppDomain.GetCurrentThreadId()是过时

INT ThreadId1 = AppDomain中。 GetCurrentThreadId(); // 1

//不是.NET的做事

INT ThreadId2 = GetCurrentThreadId方式(); // 2

//无欢试图获得我上面

INT得到了相同的结果ThreadId3 = Process.GetCurrentProcess()主题[0] .ID。 // 3
INT ThreadId4 = Thread.CurrentThread.ManagedThreadId; // 4


Console.WriteLine(ThreadId1:{0},ThreadId2:{1},ThreadId3:{2},+
ThreadId4:{3} ,
ThreadId1,ThreadId2,ThreadId3,ThreadId4);
}


解决方案

PInvoking到 GetCurrentThreadId 是你最好的选择会给你正确的信息。



不过,我必须警告你,有很好的理由在CLR不提供这样的信息:它几乎托管代码完全无用的值。这是完全合法的从一个单一的管理线程CLR视角由几个不同的本地线程进行备份期间,它的一生。这意味着 GetCurrentThreadId 的结果,整个线程的一生中可以(会)的变化。



在许多应用中,这不是一个可观察到的现象。在UI应用程序这不会真正发生,因为它通常由STA线程就不好办(通常甚至是违法的),以换出,由于COM互操作问题的支持。因此,许多开发商都幸福地无知这一点。然而,它很容易,通常是一个后台线程的执行上下文引擎盖下换出MTA线程。


As an exercise I was writing some code to display the O/S processes and O/S threads within a process (like Sysinternals process explorer does).

I found that .net's ManagedThreadId(s) are not the O/S thread ids. After a bit of reading I came across AppDomain.GetCurrentThreadId(). Unfortunately, that function is marked as "obsolete" (which could mean "not available" in the future). One solution I found is to use InteropServices to directly call the Win32 GetCurrentThreadId. I am fine with that but, it feels counter to the .net philosophy.

My question is: is there a CLR "friendly" way of obtaining the real id of the current thread ?

For reference, here is a snippet of code showing what I've tried so far. // 1 and // 2 display the correct thread id, // 3 and // 4 were attempts to obtain the same info in a CLR friendly way (but they don't work.)

Thank you for your help,

John.

[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();

static void Main(string[] args)
{
  // AppDomain.GetCurrentThreadId() is "obsolete"

  int ThreadId1 = AppDomain.GetCurrentThreadId();   // 1 

  // not the ".net" way of doing things

  int ThreadId2 = GetCurrentThreadId();             // 2 

  // "no joy" attempts to get the same results I got above

  int ThreadId3 = Process.GetCurrentProcess().Threads[0].Id;   // 3
  int ThreadId4 = Thread.CurrentThread.ManagedThreadId;        // 4


  Console.WriteLine("ThreadId1: {0}, ThreadId2: {1}, ThreadId3: {2}, " + 
                    "ThreadId4: {3}",
                    ThreadId1, ThreadId2, ThreadId3, ThreadId4);
}

解决方案

PInvoking into the GetCurrentThreadId is your best bet and will give you the correct information.

However I must warn you, there are very good reasons why the CLR doesn't provide this information: it's almost a completely useless value for managed code. It's perfectly legal from a CLR perspective for a single managed thread to be backed by several different native threads during it's lifetime. This means the result of GetCurrentThreadId can (and will) change throughout the course of a thread's lifetime.

In many applications this is not an observable phenomenon. In a UI application this won't actually happen because it's typically backed by an STA thread which is harder (usually even illegal) to swap out due to COM interop issues. So many developers are blissfully ignorant of this. However it's very easy to swap out MTA threads under the hood which is typically the execution context of a background thread.

这篇关于我如何获得_real_线程ID在CLR"友好和QUOT;办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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