Files
mirror-processhacker/1.x/trunk/ProcessHacker.Native/Objects/TimerHandle.cs
T
dmex 056f4c0914 PH1.x: Code Cleanup/Fixes
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@4784 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2011-10-30 02:47:41 +00:00

225 lines
8.4 KiB
C#

using System;
using ProcessHacker.Native.Api;
using ProcessHacker.Native.Security;
namespace ProcessHacker.Native.Objects
{
public sealed class TimerHandle : NativeHandle<TimerAccess>
{
/// <summary>
/// Creates a timer.
/// </summary>
/// <param name="access">The desired access to the timer.</param>
/// <param name="type">
/// The type of timer; synchronization timers will be reset once waiting threads are released.
/// </param>
/// <returns>A handle to the timer.</returns>
public static TimerHandle Create(TimerAccess access, TimerType type)
{
return Create(access, null, type);
}
/// <summary>
/// Creates a timer.
/// </summary>
/// <param name="access">The desired access to the timer.</param>
/// <param name="name">A name for the timer in the object manager namespace.</param>
/// <param name="type">
/// The type of timer; synchronization timers will be reset once waiting threads are released.
/// </param>
/// <returns>A handle to the timer.</returns>
public static TimerHandle Create(TimerAccess access, string name, TimerType type)
{
return Create(access, name, 0, null, type);
}
/// <summary>
/// Creates a timer.
/// </summary>
/// <param name="access">The desired access to the timer.</param>
/// <param name="name">A name for the timer in the object manager namespace.</param>
/// <param name="objectFlags">The flags to use when creating the object.</param>
/// <param name="rootDirectory">The directory in which to place the timer. This can be null.</param>
/// <param name="type">
/// The type of timer; synchronization timers will be reset once waiting threads are released.
/// </param>
/// <returns>A handle to the timer.</returns>
public static TimerHandle Create(TimerAccess access, string name, ObjectFlags objectFlags, DirectoryHandle rootDirectory, TimerType type)
{
ObjectAttributes oa = new ObjectAttributes(name, objectFlags, rootDirectory);
IntPtr handle;
try
{
Win32.NtCreateTimer(out handle, access, ref oa, type).ThrowIf();
}
finally
{
oa.Dispose();
}
return new TimerHandle(handle, true);
}
public static TimerHandle FromHandle(IntPtr handle)
{
return new TimerHandle(handle, false);
}
private TimerHandle(IntPtr handle, bool owned)
: base(handle, owned)
{ }
public TimerHandle(string name, ObjectFlags objectFlags, DirectoryHandle rootDirectory, TimerAccess access)
{
ObjectAttributes oa = new ObjectAttributes(name, objectFlags, rootDirectory);
IntPtr handle;
try
{
Win32.NtOpenTimer(out handle, access, ref oa).ThrowIf();
}
finally
{
oa.Dispose();
}
this.Handle = handle;
}
public TimerHandle(string name, TimerAccess access)
: this(name, 0, null, access)
{ }
/// <summary>
/// Cancels the timer, preventing it from being signaled.
/// </summary>
/// <returns>The state of the timer (whether it is signaled).</returns>
public bool Cancel()
{
bool currentState;
Win32.NtCancelTimer(this, out currentState).ThrowIf();
return currentState;
}
/// <summary>
/// Gets information about the timer.
/// </summary>
public TimerBasicInformation BasicInformation
{
get
{
TimerBasicInformation tbi;
int retLength;
Win32.NtQueryTimer(
this,
TimerInformationClass.TimerBasicInformation,
out tbi,
TimerBasicInformation.SizeOf,
out retLength
).ThrowIf();
return tbi;
}
}
/// <summary>
/// Sets the timer.
/// </summary>
/// <param name="dueTime">The time at which the timer is to be signaled.</param>
/// <param name="period">
/// The time interval for periodic signaling of the timer, in milliseconds.
/// Specify 0 for no periodic signaling.
/// </param>
/// <returns>The state of the timer (whether it is signaled).</returns>
public bool Set(DateTime dueTime, int period)
{
return this.Set(dueTime.ToFileTime(), false, null, IntPtr.Zero, period);
}
/// <summary>
/// Sets the timer.
/// </summary>
/// <param name="dueTime">A relative due time, in 100ns units.</param>
/// <param name="period">
/// The time interval for periodic signaling of the timer, in milliseconds.
/// Specify 0 for no periodic signaling.
/// </param>
/// <returns>The state of the timer (whether it is signaled).</returns>
public bool Set(long dueTime, int period)
{
return this.Set(dueTime, null, period);
}
/// <summary>
/// Sets the timer.
/// </summary>
/// <param name="dueTime">A relative due time, in 100ns units.</param>
/// <param name="routine">A routine to call when the timer is signaled.</param>
/// <param name="period">
/// The time interval for periodic signaling of the timer, in milliseconds.
/// Specify 0 for no periodic signaling.
/// </param>
/// <returns>The state of the timer (whether it is signaled).</returns>
public bool Set(long dueTime, TimerApcRoutine routine, int period)
{
return this.Set(dueTime, true, routine, IntPtr.Zero, period);
}
/// <summary>
/// Sets the timer.
/// </summary>
/// <param name="dueTime">A due time, in 100ns units.</param>
/// <param name="relative">Whether the due time is relative.</param>
/// <param name="routine">A routine to call when the timer is signaled.</param>
/// <param name="context">A value to pass to the timer callback routine.</param>
/// <param name="period">
/// The time interval for periodic signaling of the timer, in milliseconds.
/// Specify 0 for no periodic signaling.
/// </param>
/// <returns>The state of the timer (whether it is signaled).</returns>
public bool Set(long dueTime, bool relative, TimerApcRoutine routine, IntPtr context, int period)
{
return this.Set(dueTime, relative, routine, context, false, period);
}
/// <summary>
/// Sets the timer.
/// </summary>
/// <param name="dueTime">A due time, in 100ns units.</param>
/// <param name="relative">Whether the due time is relative.</param>
/// <param name="routine">A routine to call when the timer is signaled.</param>
/// <param name="context">A value to pass to the timer callback routine.</param>
/// <param name="resume">
/// Whether the power manager should restore the system when the timer is signaled.
/// </param>
/// <param name="period">
/// The time interval for periodic signaling of the timer, in milliseconds.
/// Specify 0 for no periodic signaling.
/// </param>
/// <returns>The state of the timer (whether it is signaled).</returns>
public bool Set(long dueTime, bool relative, TimerApcRoutine routine, IntPtr context, bool resume, int period)
{
long realDueTime = relative ? -dueTime : dueTime;
bool previousState;
// Keep the APC routine alive.
Win32.NtSetTimer(
this,
ref realDueTime,
routine,
context,
resume,
period,
out previousState
).ThrowIf();
return previousState;
}
}
}