Xom9ik

Lab_5/15var (IV semester) OS&SP

Jun 1st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. //SystemEvent.cs
  2. using System;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace Lab5
  6. {
  7.     public class SystemEvent
  8.     {
  9.         private string eventName;
  10.         private IntPtr handle;
  11.         private IntPtr attributes = IntPtr.Zero;
  12.         private bool manualReset;
  13.         private bool initialState;
  14.  
  15.         [DllImport("kernel32.dll")]
  16.         static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset,
  17.             bool bInitialState, string lpName);
  18.  
  19.         [DllImport("kernel32.dll", SetLastError = true)]
  20.         [return: MarshalAs(UnmanagedType.Bool)]
  21.         static extern bool CloseHandle(IntPtr hObject);
  22.  
  23.         [DllImport("kernel32.dll")]
  24.         static extern bool SetEvent(IntPtr hEvent);
  25.  
  26.  
  27.         [DllImport("kernel32.dll")]
  28.         static extern bool ResetEvent(IntPtr hEvent);
  29.  
  30.         [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
  31.         internal static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
  32.  
  33.  
  34.         public SystemEvent(string EventName) : this(EventName, false) { }
  35.  
  36.         public SystemEvent(string EventName, bool ManualReset)
  37.         {
  38.             eventName = EventName;
  39.             manualReset = ManualReset;
  40.             initialState = false;
  41.  
  42.             handle = CreateEvent(attributes, manualReset, initialState, eventName);
  43.         }
  44.  
  45.  
  46.         public bool Wait(int milliseconds)
  47.         {
  48.             return WaitForSingleObject(handle, milliseconds) == 0;
  49.         }
  50.  
  51.  
  52.         public void Set()
  53.         {
  54.             initialState = true;
  55.             handle = CreateEvent(attributes, manualReset, initialState, eventName);
  56.             SetEvent(handle);
  57.             CloseHandle(handle);
  58.         }
  59.  
  60.  
  61.         public void Reset()
  62.         {
  63.             initialState = false;
  64.             handle = CreateEvent(attributes, manualReset, initialState, eventName);
  65.             ResetEvent(handle);
  66.         }
  67.     }
  68. }
  69.  
  70. //Sender
  71. using System.Windows.Forms;
  72. namespace Lab5
  73. {
  74.     public partial class Lab_5_sender : Form
  75.     {
  76.         public static Lab_5_sender Instance { get; private set; }
  77.         SystemEvent OnMouseWheel = new SystemEvent("OnMouseWheel", true);
  78.  
  79.         public Lab_5_sender()
  80.         {
  81.             if (Instance == null)
  82.                 Instance = this;
  83.             InitializeComponent();
  84.             textBox1.Text = "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n";
  85.             textBox1.MouseWheel += textBox1_MouseWheel;
  86.         }
  87.         private void textBox1_MouseWheel(object sender, MouseEventArgs e)
  88.         {
  89.             OnMouseWheel.Set();
  90.         }
  91.     }
  92. }
  93.  
  94. //Receive
  95. using System;
  96. using System.Windows.Forms;
  97. using System.Threading.Tasks;
  98.  
  99. namespace Lab5
  100. {
  101.     public partial class Lab_5_receive : Form
  102.     {
  103.         SystemEvent OnMouseWheelEvent = new SystemEvent("OnMouseWheel");
  104.         public Lab_5_receive()
  105.         {
  106.             InitializeComponent();
  107.             new Lab_5_sender().Show();
  108.             WaitForEvent();
  109.         }
  110.  
  111.         private async void WaitForEvent()
  112.         {
  113.             while (true)
  114.             {
  115.                 await Task.Delay(10);
  116.                 if (OnMouseWheelEvent.Wait(10))
  117.                 {
  118.                     textBox1.AppendText(DateTime.Now + ": " + "OnMouseWheel\r\n");
  119.                     OnMouseWheelEvent.Reset();
  120.                 }
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment