This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ThreadUtility.EnterBackgroundProcessingMode.
- Loading branch information
Showing
5 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
| ||
using System; | ||
|
||
namespace Logos.Utility | ||
{ | ||
/// <summary> | ||
/// Provides utility methods for inspecting and using the current environment. | ||
/// </summary> | ||
public static class EnvironmentUtility | ||
{ | ||
/// <summary> | ||
/// Returns <c>true</c> if the current OS is Windows Vista (or Server 2008) or later. | ||
/// </summary> | ||
/// <returns><c>true</c> if the current OS is at least Windows Vista (or Server 2008).</returns> | ||
public static bool IsWindowsVistaOrLater() | ||
{ | ||
OperatingSystem os = Environment.OSVersion; | ||
return os.Platform == PlatformID.Win32NT && os.Version >= new Version(6, 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
| ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
|
||
namespace Logos.Utility | ||
{ | ||
[SuppressUnmanagedCodeSecurity] | ||
internal static class SafeNativeMethods | ||
{ | ||
[DllImport("Kernel32.dll", ExactSpelling = true)] | ||
public static extern IntPtr GetCurrentThread(); | ||
} | ||
|
||
[SuppressUnmanagedCodeSecurity] | ||
internal static class UnsafeNativeMethods | ||
{ | ||
[DllImport("Kernel32.dll", ExactSpelling = true)] | ||
[return: MarshalAs(UnmanagedType.Bool)] | ||
public static extern bool SetThreadPriority(IntPtr hThread, int nPriority); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
| ||
using System; | ||
using System.Security.Permissions; | ||
using System.Threading; | ||
|
||
namespace Logos.Utility.Threading | ||
{ | ||
/// <summary> | ||
/// Utility methods for working with threads. | ||
/// </summary> | ||
public static class ThreadUtility | ||
{ | ||
/// <summary> | ||
/// Puts the current thread into background processing mode. | ||
/// </summary> | ||
/// <returns>A Scope that must be disposed to leave background processing mode.</returns> | ||
/// <remarks>See <a href="http://code.logos.com/blog/2008/10/using_background_processing_mode_from_c.html">Using | ||
/// "Background Processing Mode" from C#</a>.</remarks> | ||
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlThread)] | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
| ||
namespace Logos.Utility | ||
{ | ||
internal static class Win32 | ||
{ | ||
public const int THREAD_MODE_BACKGROUND_BEGIN = 0x00010000; | ||
public const int THREAD_MODE_BACKGROUND_END = 0x00020000; | ||
} | ||
} |