public static Scope EnterBackgroundProcessingMode() { Thread.BeginThreadAffinity(); IntPtr hThread = SafeNativeMethods.GetCurrentThread(); if (EnvironmentUtility.IsWindowsVistaOrLater() && UnsafeNativeMethods.SetThreadPriority(hThread, Win32.THREAD_MODE_BACKGROUND_BEGIN)) { // OS supports background processing; return Scope that exits this mode return(Scope.Create(() => { UnsafeNativeMethods.SetThreadPriority(hThread, Win32.THREAD_MODE_BACKGROUND_END); Thread.EndThreadAffinity(); })); } // OS doesn't support background processing mode (or setting it failed) Thread.EndThreadAffinity(); return(Scope.Empty); }