由于AppDomin.GetCurrentThreadId()已过时
"AppDomain.GetCurrentThreadId已被弃用,因为当托管线程在光纤(也称为轻量级线程)上运行时,它不提供稳定的Id.要获取托管线程的稳定标识符,请使用Thread上的ManagedThreadId属性 .http:// go .microsoft.com/fwlink /?linkid = 14202 "
我尽量不使用它.但是,'Thread.CurrentThread.ManagedThreadId'可以解释的解释是没有价值的,因为它没有提供Win32线程ID,这是Win32调用所需要的.所以我自己实现如下.
public sealed class AppDomainExtender
{
public static int GetCurrentThreadId_New()
{
return Process.GetCurrentProcess().Threads.OfType<ProcessThread>().SingleOrDefault(x => x.ThreadState == System.Diagnostics.ThreadState.Running).Id;
}
}
Run Code Online (Sandbox Code Playgroud)
现在有两个问题.
问题: