AppDomain.GetCurrentThreadID VS Thread.ManagedThreadID的Windows API调用? [英] AppDomain.GetCurrentThreadID vs Thread.ManagedThreadID for Windows API calls?

查看:1456
本文介绍了AppDomain.GetCurrentThreadID VS Thread.ManagedThreadID的Windows API调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图创建一个钩子监视鼠标光标的当前位置。什么都不重要,我只需要计算界面设计过程中一些像素,想学习如何创建一个钩子,所以我决定去一个艰辛的道路,而不是一个理智的方式。


I'm trying to create a hook to monitor the current position of the mouse cursor. Nothing important, I just need to count some pixels during interface design and wanted to learn how to create a hook, so I decided to go for a hard way instead of a sane way.

我发现例如code的声明了以下功能:

I've found example code which declares the following function:

 <DllImport("User32.dll", CharSet:=CharSet.Auto, _
 CallingConvention:=CallingConvention.StdCall)> _
 Public Overloads Shared Function SetWindowsHookEx _
      (ByVal idHook As Integer, ByVal HookProc As CallBack, _
       ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function

当函数被调用,以下code时:

When the function is called, the following code is used:

        hHook = SetWindowsHookEx(WH_MOUSE, _
                                 hookproc, _
                                 IntPtr.Zero, _
                                 AppDomain.GetCurrentThreadId())

但Appdomain.GetCurrentThreadID生成警告:'公共共享功能GetCurrentThreadId()作为整数已过时:AppDomain.GetCurrentThreadId已去precated,因为它并没有提供在管理线程运行在一个稳定的标识纤维(又名轻量级线程)。为了得到一个稳定的标识符托管线程,使用ManagedThreadId财产上的主题。

But Appdomain.GetCurrentThreadID generates the warning: "'Public Shared Function GetCurrentThreadId() As Integer' is obsolete: 'AppDomain.GetCurrentThreadId has been deprecated because it does not provide a stable Id when managed threads are running on fibers (aka lightweight threads). To get a stable identifier for a managed thread, use the ManagedThreadId property on Thread."

我已经使用ManagedThreadID尝试过,但不起作用。返回的线程ID似乎是线程的逻辑线程ID,因为它运行在.NET运行时,而不是在Win32线程标识符

I've tried using ManagedThreadID, but that doesn't work. The thread ID returned seems to be the logical thread ID of the thread, as it runs in the .net runtime, rather than the Win32 thread identifier.

调用函数第i个AppDomain.GetCurrentThreadID的作品,但我真的想有一个稳定的标识符我的主题。

Calling the function ith AppDomain.GetCurrentThreadID works, but I really would like to have a "stable identifier" for my thread.

有人可以给我解释一下,是否可以使用ManagedThreadID在这种情况下(我假设没有),如果没有,这个事情我需要避免,以成为停止AppDomain.CurrentThreadID不稳定?

Can someone explain to me whether it is possible to use ManagedThreadID in this context (I assume not) and, if not, the things I need to avoid in order to stop the AppDomain.CurrentThreadID from becoming "unstable"?

干杯

推荐答案

有不可能使用ManagedThreadId于该上下文。这是一个完全管理的概念,并没有真正的重新presentation在本土的世界。因此,它没有任何意义的API的你传递给。

It is not possible to use ManagedThreadId in this context. This is a completely managed concept and has no real representation in the native world. Hence it doesn't make any sense to the API's you're passing it to.

原因ManagedThreadId存在,是因为有不一定是本机和托管线程之间的一对一映射。该CLR可以自由使用多个本地线程只要本地线程是一个它的替代兼容运行单个托管线程。它不能例如,在不同的COM单元。

The reason ManagedThreadId exists is because there is not necessarily a 1-1 mapping between a native and managed thread. The CLR is free to use multiple native threads to run a single managed thread as long as the native thread is compatible with the one it's replacing. It cannot for instance, be in a different COM apartment.

在某些方面,你在这里有点卡住。 AFAIK,有没有办法100%保证,你将有一个给定的托管线程在同一本地线程。你可以实现保证了很高的水平但如果你是实例上运行一个WinForms或WPF应用程序,并出现在UI线程调用本地code。原因是,这两种UI框架生活在STA公寓,这使得它非常困难(如果甚至有可能)的CLR从下切换了。

In some ways you're a bit stuck here. AFAIK, there is no way to 100% guarantee that you will have the same native thread for a given managed thread. You can achieve a very high level of guarantee though if you are for instance running a WinForms or WPF application and the call to native code occurs on the UI thread. The reason being that both of these UI frameworks live in STA apartments which makes it very hard (if even possible) for the CLR to switch out from under you.

精简版:如果你在一个WinForms或WPF应用程序,并在UI线程上运行,你可以假设稳定这个标识一个合理的水平。

Short Version: If you're in a WinForms or WPF application and running this on the UI thread, you can assume a reasonable level of stability for this identifier.

这篇关于AppDomain.GetCurrentThreadID VS Thread.ManagedThreadID的Windows API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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