• c#信号量实现线程挂起,暂停,继续,停止操作


    c#信号量实现线程挂起,暂停,继续,停止操作

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace testManualResetEvent
    {
    
        public partial class Form1 : Form
        {
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);//初始信号量状态false,线程启动后为阻塞状态
            CancellationTokenSource cts = new CancellationTokenSource();//终止线程
            Task task;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                task = new Task(run, cts.Token);
                task.Start();
            }
    
            bool flagwait = false;
            private void run()
            {
                int i = 0;
                while (true)
                {
                    if (flagwait)
                    {
                        #region
                        /* 
                         * WaiOne函数会暂停当前线程
                         * 继续条件有两个:
                         * 1、参数1指定的时间到了,如:5000ms(此时忽略信号量状态)
                         * 2、信号量变为置位状态。
                         * 
                         * 如果调用WaiOne的无参函数,则无限等待,继续条件为:
                         * 信号量变为置位状态。
                         */
                        #endregion
    
                        manualResetEvent.Reset();//先复位信号量(信号量置位情况下,未复位无法阻塞线程)
                        bool flag = manualResetEvent.WaitOne(1000, false);//不指定时间则无限暂停
    
                        flagwait = false;
                    }
    
                    
                    manualResetEvent.WaitOne();//不指定时间则无限暂停
    
                    this.Invoke(new Action<string>((s) => { label1.Text = s; }), i++.ToString());
    
                    #region 线程终止CancellationTokenSource
                    if (cts.Token.IsCancellationRequested)
                    {
                        Console.WriteLine("线程被终止!");
                        break;
                    }
                    #endregion
    
    
                    Thread.Sleep(1);
                }
            }
    
            private void button_阻塞线程_Click(object sender, EventArgs e)
            {
                flagwait = true;
            }
    
            private void button_信号量置位_Click(object sender, EventArgs e)
            {
                manualResetEvent.Set();
            }
    
            private void button_信号量复位_Click(object sender, EventArgs e)
            {
                manualResetEvent.Reset();
            }
    
            private void button_CancellationTokenSource终止线程_Click(object sender, EventArgs e)
            {
                cts.Token.ThrowIfCancellationRequested();
                cts.Cancel();
            }
        }
    }
    欢迎讨论,相互学习。 txwtech@163.com
  • 相关阅读:
    sqlserver2008导出表结构和数据
    使用adb命令对手机进行截屏保存到电脑
    android中控制多点同时触发时间
    使用Androi自带模拟器7.0版本无法安装apk解决
    Android library使用butterknife配置
    使用RadioGroup和fragment搭建项目框架填坑
    【转】BaseAdapter&DataSetObserver通知机制
    【转】读BaseAdapter的一点感悟
    使用Rxjava和Retrofit报错--01
    使用LeakCanary检测内存泄漏
  • 原文地址:https://www.cnblogs.com/txwtech/p/15238030.html
Copyright © 2020-2023  润新知