Thread.CurrentThread.ManagedThreadId和AppDomain.GetCurrentThreadId()之间的区别

时间:2017-07-24 07:48:56

标签: c# multithreading

我正在运行此代码:

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

输出:

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

两个ID之间有什么区别?

1 个答案:

答案 0 :(得分:1)

AppDomain.GetCurrentThreadId()已过时:

'AppDomain.GetCurrentThreadId已被弃用,因为当托管线程在光纤(又称轻量级线程)上运行时,它不提供稳定的ID。要获取托管线程的稳定标识符,请使用ManagedThreadId上的Thread属性。

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