C#中ManualResetEvent和AutoResetEvent的基本使用
AutoResetEvent
行为
:当 AutoResetEvent 的 Set 方法被调用时,它将允许一个正在等待的线程继续执行。一旦线程继续执行,AutoResetEvent 的状态将自动重置为未信号状态,阻止其他等待的线程继续执行,直到 Set 方法再次被调用。
用途
:通常用于通知单个等待线程某个条件已经满足。
ManualResetEvent
行为
:当 ManualResetEvent 的 Set 方法被调用时,它将允许所有正在等待的线程继续执行。与 AutoResetEvent 不同,ManualResetEvent 的状态在允许一个或多个线程继续执行后不会自动重置。要重置其状态(即阻止更多线程继续执行),必须显式调用 Reset 方法。
用途
:通常用于通知多个等待线程某个条件已经满足,或者需要让多个线程在某个点同时继续执行。
AutoResetEvent
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp5
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public static AutoResetEvent camImageEvent1 = new AutoResetEvent(false);
public static AutoResetEvent camImageEvent2 = new AutoResetEvent(false);
public static AutoResetEvent camImageEvent3 = new AutoResetEvent(false);
public static AutoResetEvent camImageEvent4 = new AutoResetEvent(false);
public static AutoResetEvent commEvent1 = new AutoResetEvent(false);
public static AutoResetEvent commEvent2 = new AutoResetEvent(false);
public MainWindow()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Run(Inspection1);
Task.Run(Inspection2);
}
private void Inspection1()
{
List<WaitHandle> CameraEWaitHandles = new List<WaitHandle>() { camImageEvent1, commEvent1 };
while (true)
{
bool bTimeOut = WaitHandle.WaitAll(CameraEWaitHandles.ToArray(), 9000);
if (!bTimeOut)
continue;
MessageBox.Show("触发1");
}
}
private void Inspection2()
{
List<WaitHandle> CameraEWaitHandles = new List<WaitHandle>() { camImageEvent2, commEvent2 };
while (true)
{
bool bTimeOut = WaitHandle.WaitAll(CameraEWaitHandles.ToArray(), 9000);
if (!bTimeOut)
continue;
MessageBox.Show("触发2");
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
camImageEvent1.Set();
commEvent1.Set();
commEvent2.Set();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
camImageEvent2.Set();
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
}
}
}
ManualResetEvent
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp23
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public static ManualResetEvent camImageEvent1 = new ManualResetEvent(false);
public static ManualResetEvent camImageEvent2 = new ManualResetEvent(false);
public static ManualResetEvent camImageEvent3 = new ManualResetEvent(false);
public static ManualResetEvent camImageEvent4 = new ManualResetEvent(false);
public static ManualResetEvent commEvent1 = new ManualResetEvent(false);
public static ManualResetEvent commEvent2 = new ManualResetEvent(false);
public MainWindow()
{
InitializeComponent();
}
private void Inspection1()
{
List<WaitHandle> CameraEWaitHandles = new List<WaitHandle>() { camImageEvent1, commEvent1 };
while (true)
{
bool bTimeOut = WaitHandle.WaitAll(CameraEWaitHandles.ToArray(), 9000);
if (!bTimeOut)
continue;
MessageBox.Show("触发1");
commEvent1.Reset();
}
}
private void Inspection2()
{
List<WaitHandle> CameraEWaitHandles = new List<WaitHandle>() { camImageEvent2, commEvent2 };
while (true)
{
bool bTimeOut = WaitHandle.WaitAll(CameraEWaitHandles.ToArray(), 9000);
if (!bTimeOut)
continue;
MessageBox.Show("触发2");
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Run(Inspection1);
Task.Run(Inspection2);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
camImageEvent1.Set();
commEvent1.Set();
commEvent2.Set();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
camImageEvent2.Set();
}
}
}
一起使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp23
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public static AutoResetEvent camImageEvent1 = new AutoResetEvent(false);
public static AutoResetEvent camImageEvent2 = new AutoResetEvent(false);
public static AutoResetEvent camImageEvent3 = new AutoResetEvent(false);
public static AutoResetEvent camImageEvent4 = new AutoResetEvent(false);
public static ManualResetEvent commEvent1 = new ManualResetEvent(false);
public static ManualResetEvent commEvent2 = new ManualResetEvent(false);
public MainWindow()
{
InitializeComponent();
}
private void Inspection1()
{
List<WaitHandle> CameraEWaitHandles = new List<WaitHandle>() { camImageEvent1, commEvent1 };
while (true)
{
bool bTimeOut = WaitHandle.WaitAll(CameraEWaitHandles.ToArray(), 9000);
if (!bTimeOut)
continue;
MessageBox.Show("触发1");
commEvent1.Reset();
}
}
private void Inspection2()
{
List<WaitHandle> CameraEWaitHandles = new List<WaitHandle>() { camImageEvent2, commEvent2 };
while (true)
{
bool bTimeOut = WaitHandle.WaitAll(CameraEWaitHandles.ToArray(), 9000);
if (!bTimeOut)
continue;
MessageBox.Show("触发2");
camImageEvent2.Reset();
commEvent2.Reset();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Run(Inspection1);
Task.Run(Inspection2);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
camImageEvent1.Set();
commEvent1.Set();
commEvent1.Set();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
camImageEvent2.Set();
}
}
}