zitjubiz

开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

 

分类:

技术点:

相关文章:

  • 2021-11-20
  • 2021-05-17
  • 2021-10-21
  • 2021-11-21
  • 2022-01-07
  • 2022-01-08
  • 2022-02-12
猜你喜欢
  • 2021-07-29
  • 2022-01-18
  • 2021-12-27
  • 2021-10-15
  • 2022-02-05
  • 2021-08-24
  • 2022-01-06
相关资源
相似解决方案