private static SafeWaitHandle GetOSHandleForCurrentThread() { IntPtr currentProcHandle = Interop.mincore.GetCurrentProcess(); IntPtr currentThreadHandle = Interop.mincore.GetCurrentThread(); SafeWaitHandle threadHandle; if (Interop.mincore.DuplicateHandle(currentProcHandle, currentThreadHandle, currentProcHandle, out threadHandle, 0, false, (uint)Interop.Constants.DuplicateSameAccess)) { return(threadHandle); } // Throw an ApplicationException for compatibility with CoreCLR. First save the error code. int errorCode = Marshal.GetLastWin32Error(); var ex = new ApplicationException(); ex.SetErrorCode(errorCode); throw ex; }