title: AppDomain.GetCurrentThreadID vs Thread.ManagedThreadID for Windows API calls?
body: <p>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.</p>
  
  <p>I've found example code which declares the following function:</p>
  
  <pre><code> &lt;DllImport("User32.dll", CharSet:=CharSet.Auto, _
   CallingConvention:=CallingConvention.StdCall)&gt; _
   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></pre>
  
  <p>When the function is called, the following code is used:</p>
  
  <pre><code>        hHook = SetWindowsHookEx(WH_MOUSE, _
                                   hookproc, _
                                   IntPtr.Zero, _
                                   AppDomain.GetCurrentThreadId())
  </code></pre>
  
  <p>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."</p>
  
  <p>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.</p>
  
  <p>Calling the function ith AppDomain.GetCurrentThreadID works, but I really would like to have a "stable identifier" for my thread.</p>
  
  <p>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"?</p>
  
  <p>Cheers</p>