1. 程式人生 > >C#中如何判斷當前執行緒是否為主執行緒

C#中如何判斷當前執行緒是否為主執行緒

/ Do this when you start your application
static int mainThreadId;

// In Main method:
mainThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;

// If called in the non main thread, will return false;
public static bool IsMainThread
{
    get { return System.Threading.Thread.CurrentThread.ManagedThreadId
== mainThreadId; } }