开2个线程, 在winform界面有个label, 第1个线程负责假如是label是奇数就+1; 第2个线程负责,假如label是偶数就加1; 希望2个线程交替执行, 从0加到50; 界面上的label也要依次变化
public static ManualResetEvent mr1 = new ManualResetEvent(false);//工位1阻塞信号 默认堵塞 public static ManualResetEvent mr2 = new ManualResetEvent(false);//工位2阻塞信号 默认堵塞 int CurrentNum = frm.GetText().ToInt(); while (CurrentNum < 50) { CurrentNum = frm.GetText().ToInt(); //偶数时,左工位线程执行+1 if (CurrentNum % 2 == 0 && nFix == 1) { mr1.WaitOne(); frm.SetText((CurrentNum + 1).ToString()); Console.WriteLine("左工位:" + (CurrentNum + 1)); mr2.Set(); } //奇数时,右工位线程执行+1 if (CurrentNum % 2 != 0 && nFix == 2) { mr2.WaitOne(); frm.SetText((CurrentNum + 1).ToString()); Console.WriteLine("右工位:" + (CurrentNum + 1)); mr1.Set(); } }
参考: https://www.bilibili.com/video/BV1gv411E7LH
https://www.cnblogs.com/masonlu/p/11470965.html