public IDisposable CreateThread(ThreadStart action) { uint threadId; var duplicatedToken = IntPtr.Zero; Win32NativeMethods.DuplicateToken(logonToken, ImpersonationLevel.SecurityImpersonation, ref duplicatedToken); threadHandle = ThreadExtensions.CreateThread(() => { if (!ThreadExtensions.SetThreadToken(IntPtr.Zero, duplicatedToken)) { throw new Exception("Unable to run thread as impersonated logon!"); } action(); }, out threadId); return(new ThreadDisposable(threadHandle, threadId)); }
public void Dispose() { ThreadExtensions.CloseHandle(threadHandle); }