Wednesday, April 22, 2009

Get ThreadID and AppDomain Name and AppDomainID

Example as following:

using System;
using Sytem.Threading;

static void Main(string[] args)
{
Thread thread=new Thread(new ThreadStart(SomeMethod));
thread.Start();
}

static void SomeMethod()
{
Console.WriteLine ("Thread {0} started in {1} with AppDomainID={2}",
Thread.CurrentThread.ManagedThreadID.ToString(),
Thread.GetDomain().FriendlyName,
Thread.GetDomainID().ToString());
}

Notice to get the ThreadID, we no longer use AppDomain.GetCurrentThreadId() method. But we use Thread.CurrentThread.ManagedThreadID property. Because the former one does not get a stable ThreadID.

No comments: