C# GlobalResetEvent / WaitHandle,
GlobalResetEvent(全域重設事件)是由我編寫的一種類似與.NET線程處理方面
的AutoResetEvent(自動重設事件)、ManualResetEvent(手動重設事件)的函數集合
ARE && MRE在.NET中被規範用於線程處理方面,不過在底層它是對系統EVENT
核心對象一個函數封裝集合,C++開發人員在學習線程時有所涉及、
全域域於Mutex(互斥體)、亦稱互斥鎖 它同屬於.NET線程處理方面與lock關鍵字
功能相對類似、
由於代碼規範使然,GRE 與 ARE && MRE一致依賴於抽象WaitHandle
允許線程通過發送訊號相互連信,通常此方式涉及線程需獨佔訪問資源
調用WaitHandle::WaitOne等待訊號,內部調用WaitForSingleObject
函數等待核心對象、"生生繁華於枯荑 萋萋空翠自靈犀"
如果WaitHandle處於非終止狀態則該線程阻塞,並等待所控制資源的
線程,通過調用WaitHandle::Set /SetEvent發出可用訊號
可以通過將一個布爾值傳遞給建構函式來控制GRE && ARE && MRE
的初始狀態,如果初始狀態為終止狀態,則為true;否則為 false
調用WaitHandle::Reset /ResetEvent後終止狀態被重設為 false,
WaitHandle::Set後終止狀態設定為true
上述兩圖是利用GRE判斷進程是否啟動並執行一個列子,如果沒有在系統內檢查相關詞
條的事件運行MainForm否則WebForm,MainForm在觸發OnLoad函數後建立GRE對象
此處與ARE && MRE相似、用於線程處理方面,不過你可以理解鎖裝的代碼是
ARE方式,當然也可以修改為MRE方式、
CreateEvent第二參數bManualReset為一個布爾值,如果true 使用MRE方式否
則 false 使用ARE方式
範例程式碼:
public static class Program { private static GlobalResetEvent greUnk = new GlobalResetEvent("gre_unk", false); [STAThread] public static void Main() { ThreadPool.QueueUserWorkItem(delegate { Thread.Sleep(1200); Console.WriteLine("test."); greUnk.Set(); }); greUnk.WaitOne(); Console.WriteLine("ok."); Console.ReadKey(false); } }
GRE代碼:
#pragma warning disable 0618namespace Themyth_Chrome.Thread{ using Microsoft.Win32.SafeHandles; using System; using System.Runtime.InteropServices; using System.Threading; public partial class GlobalResetEvent : WaitHandle, IDisposable { private abstract partial class NativeMethod { [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool CloseHandle(IntPtr hObject); [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr OpenEvent(uint dwDesiredAccess, bool bInheritHandle, string lpName); [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr CreateEvent(int lpEventAttributes, bool bManualReset, bool bInitialState, string lpName); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool SetEvent(IntPtr hEvent); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool ResetEvent(IntPtr hEvent); [DllImport("kernel32.dll", SetLastError = true)] public static extern int WaitForSingleObject(IntPtr hHandle, int dwMilliseconds); public const int WAIT_OBJECT_0 = 0; public const int NULL = 0; public const int INFINITE = -1; public const int EVENT_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | (SYNCHRONIZE | 3)); public const int STANDARD_RIGHTS_REQUIRED = 983040; public const int SYNCHRONIZE = 1048576; } } public partial class GlobalResetEvent { public override void Close() { this.SafeWaitHandle.Dispose(); NativeMethod.CloseHandle(Handle); } public bool Set() { return NativeMethod.SetEvent(Handle); } public bool Reset() { return NativeMethod.ResetEvent(Handle); } public override bool WaitOne() { return this.WaitOne(NativeMethod.INFINITE); } public override bool WaitOne(int millisecondsTimeout) { return NativeMethod.WaitForSingleObject(Handle, millisecondsTimeout) == NativeMethod.WAIT_OBJECT_0; } } public partial class GlobalResetEvent { public GlobalResetEvent(IntPtr eventHandle) { if (eventHandle == IntPtr.Zero) throw new Exception("event handle is invalid."); this.Handle = eventHandle; this.SafeWaitHandle = new SafeWaitHandle(eventHandle, true); } public GlobalResetEvent(string name, bool initialState) { if ((this.Handle = NativeMethod.CreateEvent(NativeMethod.NULL, false, initialState, name)) == IntPtr.Zero) throw new Exception("create event failed."); this.SafeWaitHandle = new SafeWaitHandle(Handle, true); } public static GlobalResetEvent Open(string name) { IntPtr hUnkEvent = NativeMethod.OpenEvent(NativeMethod.EVENT_ALL_ACCESS, false, name); if (hUnkEvent == IntPtr.Zero) throw new Exception("open event failed."); return new GlobalResetEvent(hUnkEvent); } public static bool OpenExisting(string name) { IntPtr hUnkEvent = NativeMethod.OpenEvent(NativeMethod.EVENT_ALL_ACCESS, false, name); return hUnkEvent != IntPtr.Zero; } public static GlobalResetEvent Create(IntPtr eventHandle) { return new GlobalResetEvent(eventHandle); } public static GlobalResetEvent Create(string name, bool initialState) { return new GlobalResetEvent(name, initialState); } }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。