Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Commit

Permalink
Added ThreadUtility.EnterBackgroundProcessingMode.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Feb 4, 2010
1 parent ece11f2 commit aff58cb
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Logos.Utility/EnvironmentUtility.cs
@@ -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);
}
}
}
4 changes: 4 additions & 0 deletions src/Logos.Utility/Logos.Utility.csproj
Expand Up @@ -51,13 +51,17 @@
<Compile Include="DictionaryUtility.cs" />
<Compile Include="DisposableService.cs" />
<Compile Include="DisposableUtility.cs" />
<Compile Include="EnvironmentUtility.cs" />
<Compile Include="HashCodeUtility.cs" />
<Compile Include="IO\WrappingStream.cs" />
<Compile Include="NativeMethods.cs" />
<Compile Include="ObjectImpl.cs" />
<Compile Include="ObjectUtility.cs" />
<Compile Include="Ownership.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scope.cs" />
<Compile Include="Threading\ThreadUtility.cs" />
<Compile Include="Win32.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
22 changes: 22 additions & 0 deletions src/Logos.Utility/NativeMethods.cs
@@ -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);
}
}
40 changes: 40 additions & 0 deletions src/Logos.Utility/Threading/ThreadUtility.cs
@@ -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;
}
}
}
9 changes: 9 additions & 0 deletions src/Logos.Utility/Win32.cs
@@ -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;
}
}

0 comments on commit aff58cb

Please sign in to comment.