Mathematics
Mathematics

Reputation: 7628

Difference between Thread.CurrentThread.ManagedThreadId and AppDomain.GetCurrentThreadId()

I am running this code:

Parallel.For(0, 8, new ParallelOptions { MaxDegreeOfParallelism = 8 },
    i => {
         Console.WriteLine(i +
             " : Thread ManagedThreadId: " + Thread.CurrentThread.ManagedThreadId +
             " ID: " + AppDomain.GetCurrentThreadId().ToString());
    }
)

With output:

1 : Thread ManagedThreadId: 10 ID: 9004  
2 : Thread ManagedThreadId:10 ID: 9004  
0 : Thread ManagedThreadId: 7 ID: 12884  
4 : ThreadManagedThreadId: 7 ID: 12884  
5 : Thread ManagedThreadId: 7 ID: 12884

6 : Thread ManagedThreadId: 7 ID: 12884  
7 : Thread ManagedThreadId: 7 ID: 12884  
3 : Thread ManagedThreadId: 10 ID: 9004

What is the difference between the two ids?

Upvotes: 1

Views: 9133

Answers (1)

Stephen LAI
Stephen LAI

Reputation: 71

AppDomain.GetCurrentThreadId() 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.

http://go.microsoft.com/fwlink/?linkid=14202'

Upvotes: 2

Related Questions