AutoEvent(不重置产生的隐患-演示Demo)
不重置产生的隐患
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
{
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();
}
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)
{
}
}
}

只弹一个框
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
{
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();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Run(Inspection1);
Task.Run(Inspection2);
}
private void Inspection1()
{
List<WaitHandle> CameraEWaitHandles = new List<WaitHandle>() { 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>() { commEvent1 };
while (true)
{
bool bTimeOut = WaitHandle.WaitAll(CameraEWaitHandles.ToArray(), 9000);
if (!bTimeOut)
continue;
MessageBox.Show("触发2");
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
commEvent1.Set();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
}
}
}
