c00*_*0fd 7 .net c# windows synchronization timer
在非托管的Win32世界中,我习惯使用CreateWaitableTimer API 创建的等待计时器,可以用于WaitForSingleObject等同步调用,主要用于WaitForMultipleObjects.
在.NET和C#中必须有一个模拟等待计时器?
你需要什么等待计时器?
.NET中"我可以等待的东西"的默认类是System.Threading.Tasks.Task.在.NET 4.5中,您可以简单地使用Task.Delay(milliseconds).
在.NET 4.0中,您可以使用a TaskCompletionSource创建任务,并使用普通计时器将其设置为已完成.或者使用TaskEx.Delay从实现异步定位包或AsyncBridge
如果您需要实际的WaitHandle,可以使用a ManualResetEvent,并使用普通的.NET计时器来设置事件.
或者,您可以创建自己的派生自WaitHandleP/Invoke的类CreateWaitableTimer.似乎这样的类已经存在于.NET框架中,但它是内部的.(System.Runtime.IOThreadTimer.WaitableTimer来自System.ServiceModel.Internals.dll)
public class WaitableTimer : WaitHandle
{
[DllImport("kernel32.dll")]
static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, [MarshalAs(UnmanagedType.Bool)] bool fResume);
public WaitableTimer(bool manualReset = true, string timerName = null)
{
this.SafeWaitHandle = CreateWaitableTimer(IntPtr.Zero, manualReset, timerName);
}
public void Set(long dueTime)
{
if (!SetWaitableTimer(this.SafeWaitHandle, ref dueTime, 0, IntPtr.Zero, IntPtr.Zero, false))
{
throw new Win32Exception();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2616 次 |
| 最近记录: |