Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- namespace event1
- {
- class NamedEvent
- {
- private string _EventName;
- private IntPtr _Handle;
- private IntPtr _Attributes = IntPtr.Zero;
- private bool _ManualReset;
- private bool _InitialState;
- public static uint INFINITE = 0xFFFFFFFF;
- [DllImport("kernel32.dll")]
- static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- static extern bool CloseHandle(IntPtr hObject);
- [DllImport("kernel32.dll")]
- static extern bool SetEvent(IntPtr hEvent);
- [DllImport("kernel32.dll")]
- static extern bool ResetEvent(IntPtr hEvent);
- [DllImport("kernel32.dll")]
- static extern bool PulseEvent(IntPtr hEvent);
- [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
- internal static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
- /// <summary>
- /// Create a NamedEvent object with the name of the event, and assume auto reset with
- /// an initial state of reset.
- /// </summary>
- public NamedEvent(string EventName) : this(EventName, false) { }
- /// <summary>
- /// Create a NamedEvent object with the name of the event and the auto reset property,
- /// assuming an initial state of reset.
- /// </summary>
- public NamedEvent(string EventName, bool ManualReset)
- {
- _EventName = EventName;
- _ManualReset = ManualReset;
- _InitialState = false;
- }
- /// <summary>
- /// Wait for the event to signal to a maximum period of TimeoutInSecs total seconds.
- /// Returns true if the event signaled, false if timeout occurred.
- /// </summary>
- public bool Wait(int TimeoutInSecs)
- {
- _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
- int rc = WaitForSingleObject(_Handle, TimeoutInSecs * 1000);
- CloseHandle(_Handle);
- return rc == 0;
- }
- /// <summary>
- /// Pulse the named event, which results in a single waiting thread to exit the Wait method.
- /// </summary>
- public bool Pulse()
- {
- _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
- PulseEvent(_Handle);
- CloseHandle(_Handle);
- return _Handle != IntPtr.Zero;
- }
- /// <summary>
- /// Set the named event to a signaled state. The Wait() method will not block any
- /// thread as long as the event is in a signaled state.
- /// </summary>
- public void Set()
- {
- _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
- SetEvent(_Handle);
- CloseHandle(_Handle);
- }
- /// <summary>
- /// Reset the named event to a non signaled state. The Wait() method will block
- /// any thread that enters it as long as the event is in a non signaled state.
- /// </summary>
- public void Reset()
- {
- _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
- ResetEvent(_Handle);
- CloseHandle(_Handle);
- }
- /// <summary>
- /// Wait for the event with the given name to signal to a maximum period of TimeoutInSecs total seconds.
- /// Returns true if the event signaled, false if timeout occurred.
- /// </summary>
- static public bool Wait(int TimeoutInSecs, string Name)
- {
- return (new NamedEvent(Name)).Wait(TimeoutInSecs);
- }
- /// <summary>
- /// Pulse the event with the given name, which results in a single waiting thread to exit the Wait method.
- /// </summary>
- static public bool Pulse(string Name) { return (new NamedEvent(Name)).Pulse(); }
- /// <summary>
- /// Set the event with the given name to a signaled state. The Wait() method will not block
- /// any threads as long as the event is in a signaled state.
- /// </summary>
- static public void Set(string Name) { (new NamedEvent(Name)).Set(); }
- /// <summary>
- /// Reset the event with the given name to a non signaled state. The Wait() method will block
- /// any thread that enters it as long as the event is in a non signaled state.
- /// </summary>
- static public void Reset(string Name) { (new NamedEvent(Name)).Reset(); }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement