Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SystemEvent.cs
- using System;
- using System.Runtime.InteropServices;
- namespace Lab5
- {
- public class SystemEvent
- {
- private string eventName;
- private IntPtr handle;
- private IntPtr attributes = IntPtr.Zero;
- private bool manualReset;
- private bool initialState;
- [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", SetLastError = true, ExactSpelling = true)]
- internal static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
- public SystemEvent(string EventName) : this(EventName, false) { }
- public SystemEvent(string EventName, bool ManualReset)
- {
- eventName = EventName;
- manualReset = ManualReset;
- initialState = false;
- handle = CreateEvent(attributes, manualReset, initialState, eventName);
- }
- public bool Wait(int milliseconds)
- {
- return WaitForSingleObject(handle, milliseconds) == 0;
- }
- public void Set()
- {
- initialState = true;
- handle = CreateEvent(attributes, manualReset, initialState, eventName);
- SetEvent(handle);
- CloseHandle(handle);
- }
- public void Reset()
- {
- initialState = false;
- handle = CreateEvent(attributes, manualReset, initialState, eventName);
- ResetEvent(handle);
- }
- }
- }
- //Sender
- using System.Windows.Forms;
- namespace Lab5
- {
- public partial class Lab_5_sender : Form
- {
- public static Lab_5_sender Instance { get; private set; }
- SystemEvent OnMouseWheel = new SystemEvent("OnMouseWheel", true);
- public Lab_5_sender()
- {
- if (Instance == null)
- Instance = this;
- InitializeComponent();
- textBox1.Text = "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n";
- textBox1.MouseWheel += textBox1_MouseWheel;
- }
- private void textBox1_MouseWheel(object sender, MouseEventArgs e)
- {
- OnMouseWheel.Set();
- }
- }
- }
- //Receive
- using System;
- using System.Windows.Forms;
- using System.Threading.Tasks;
- namespace Lab5
- {
- public partial class Lab_5_receive : Form
- {
- SystemEvent OnMouseWheelEvent = new SystemEvent("OnMouseWheel");
- public Lab_5_receive()
- {
- InitializeComponent();
- new Lab_5_sender().Show();
- WaitForEvent();
- }
- private async void WaitForEvent()
- {
- while (true)
- {
- await Task.Delay(10);
- if (OnMouseWheelEvent.Wait(10))
- {
- textBox1.AppendText(DateTime.Now + ": " + "OnMouseWheel\r\n");
- OnMouseWheelEvent.Reset();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment