How to

get thread id

Published: 25. October 2011 | Updated: 25. October 2011
License: Microsoft Public License (MS-PL)
Categories: Windows » Win32
Tags: C# Threading Win32 Windows
Was this snippet helpful for you? YESYES / NONO

Import namespaces

using System.Runtime.InteropServices;
using System.Threading;

External method

[DllImport("Kernel32", EntryPoint = "GetCurrentThreadId", ExactSpelling = true)]
public static extern Int32 GetCurrentWin32ThreadId();

Code

int threadId = GetCurrentWin32ThreadId();
int managedThreadId = Thread.CurrentThread.ManagedThreadId;

Console.WriteLine("Thread Id: {0}", threadId);
Console.WriteLine("Managed Thread Id: {0}", managedThreadId);
Console Output:
Thread Id: 5908
Managed Thread Id: 10
Send us feedback about this snippet »



Related Snippets: