Advertisement
Guest User

Untitled

a guest
Jul 24th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.InteropServices;
  7. namespace event1
  8. {
  9.     class NamedEvent
  10.     {
  11.         private string _EventName;
  12.         private IntPtr _Handle;
  13.         private IntPtr _Attributes = IntPtr.Zero;
  14.         private bool _ManualReset;
  15.         private bool _InitialState;
  16.         public static uint INFINITE = 0xFFFFFFFF;
  17.         [DllImport("kernel32.dll")]
  18.         static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
  19.         [DllImport("kernel32.dll", SetLastError = true)]
  20.         [return: MarshalAs(UnmanagedType.Bool)]
  21.         static extern bool CloseHandle(IntPtr hObject);
  22.         [DllImport("kernel32.dll")]
  23.         static extern bool SetEvent(IntPtr hEvent);
  24.         [DllImport("kernel32.dll")]
  25.         static extern bool ResetEvent(IntPtr hEvent);
  26.         [DllImport("kernel32.dll")]
  27.         static extern bool PulseEvent(IntPtr hEvent);
  28.         [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
  29.         internal static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
  30.         /// <summary>
  31.         /// Create a NamedEvent object with the name of the event, and assume auto reset with
  32.         /// an initial state of reset.
  33.         /// </summary>
  34.         public NamedEvent(string EventName) : this(EventName, false) { }
  35.  
  36.         /// <summary>
  37.         /// Create a NamedEvent object with the name of the event and the auto reset property,
  38.         /// assuming an initial state of reset.
  39.         /// </summary>
  40.         public NamedEvent(string EventName, bool ManualReset)
  41.         {
  42.             _EventName = EventName;
  43.             _ManualReset = ManualReset;
  44.             _InitialState = false;
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Wait for the event to signal to a maximum period of TimeoutInSecs total seconds.
  49.         /// Returns true if the event signaled, false if timeout occurred.
  50.         /// </summary>
  51.         public bool Wait(int TimeoutInSecs)
  52.         {
  53.             _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
  54.             int rc = WaitForSingleObject(_Handle, TimeoutInSecs * 1000);
  55.             CloseHandle(_Handle);
  56.             return rc == 0;
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Pulse the named event, which results in a single waiting thread to exit the Wait method.
  61.         /// </summary>
  62.         public bool Pulse()
  63.         {
  64.             _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
  65.             PulseEvent(_Handle);
  66.             CloseHandle(_Handle);
  67.             return _Handle != IntPtr.Zero;
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Set the named event to a signaled state. The Wait() method will not block any
  72.         /// thread as long as the event is in a signaled state.
  73.         /// </summary>
  74.         public void Set()
  75.         {
  76.             _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
  77.             SetEvent(_Handle);
  78.             CloseHandle(_Handle);
  79.         }
  80.  
  81.         /// <summary>
  82.         /// Reset the named event to a non signaled state. The Wait() method will block
  83.         /// any thread that enters it as long as the event is in a non signaled state.
  84.         /// </summary>
  85.         public void Reset()
  86.         {
  87.             _Handle = CreateEvent(_Attributes, _ManualReset, _InitialState, _EventName);
  88.             ResetEvent(_Handle);
  89.             CloseHandle(_Handle);
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Wait for the event with the given name to signal to a maximum period of TimeoutInSecs total seconds.
  94.         /// Returns true if the event signaled, false if timeout occurred.
  95.         /// </summary>
  96.         static public bool Wait(int TimeoutInSecs, string Name)
  97.         {
  98.             return (new NamedEvent(Name)).Wait(TimeoutInSecs);
  99.         }
  100.  
  101.         /// <summary>
  102.         /// Pulse the event with the given name, which results in a single waiting thread to exit the Wait method.
  103.         /// </summary>
  104.         static public bool Pulse(string Name) { return (new NamedEvent(Name)).Pulse(); }
  105.  
  106.         /// <summary>
  107.         /// Set the event with the given name to a signaled state. The Wait() method will not block
  108.         /// any threads as long as the event is in a signaled state.
  109.         /// </summary>
  110.         static public void Set(string Name) { (new NamedEvent(Name)).Set(); }
  111.  
  112.         /// <summary>
  113.         /// Reset the event with the given name to a non signaled state. The Wait() method will block
  114.         /// any thread that enters it as long as the event is in a non signaled state.
  115.         /// </summary>
  116.         static public void Reset(string Name) { (new NamedEvent(Name)).Reset(); }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement