C# Task 多线程 开始 暂停 停止

C# Task 多线程 开始 暂停 停止

  public partial class Form1 : Form
    {

        private ManualResetEvent ma;
        private CancellationTokenSource tokenSource;
        private CancellationToken token;

        static readonly object objLock = new object();

        private bool IsStart = false;
        private bool IsManualResetEvent = true;
        public Form1()
        {
            ma = new ManualResetEvent(IsManualResetEvent);
            tokenSource = new CancellationTokenSource();
            token = tokenSource.Token;
            InitializeComponent();
        }

        /// 
        /// 开始
        /// 
        /// 
        /// 
        private void button1_Click(object sender, EventArgs e)
        {
            if (IsStart)
            {
                MessageBox.Show("程序已经启动!");
                return;
            }
            else
            {

                tokenSource = new CancellationTokenSource();
                token = tokenSource.Token;
                IsStart = true;
                Task.Run(() => Start01(), tokenSource.Token);
                Task.Run(() => Start02(), tokenSource.Token);
                Task.Run(() => Start03(), tokenSource.Token);
                Task.Run(() => Start04(), tokenSource.Token);




            }



        }

        public void Start01()
        {
            for (int i = 0; i < 9999; i++)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                string str = "计数器:" + i;
                Invoke(new Action(() => { this.richTextBox1.Text = str; }));
                ma.WaitOne();
                Thread.Sleep(100);
            }
        }
        public void Start02()
        {
            for (int i = 0; i < 9999; i++)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                string str = "计数器:" + i;
                Invoke(new Action(() => { this.richTextBox2.Text = str; }));
                ma.WaitOne();
                Thread.Sleep(100);
            }
        }
        public void Start03()
        {
            for (int i = 0; i < 9999; i++)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                string str = "计数器:" + i;
                Invoke(new Action(() => { this.richTextBox3.Text = str; }));
                ma.WaitOne();
                Thread.Sleep(100);
            }
        }
        public void Start04()
        {
            for (int i = 0; i < 9999; i++)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                string str = "计数器:" + i;
                Invoke(new Action(() => { this.richTextBox4.Text = str; }));
                ma.WaitOne();
                Thread.Sleep(100);
            }
        }








        /// 
        /// 暂停
        /// 
        /// 
        /// 
        private void button3_Click(object sender, EventArgs e)
        {

            if (IsStart)
            {
                ma.Reset();
            }

        }

        /// 
        /// 继续
        /// 
        /// 
        /// 
        private void button4_Click(object sender, EventArgs e)
        {
            if (IsStart)
            {
                ma.Set();
            }

        }

        /// 
        /// 停止
        /// 
        /// 
        /// 
        private void button2_Click(object sender, EventArgs e)
        {
            //线程取消 + 加上取消回调
            tokenSource.Cancel();
            tokenSource.Token.Register(() =>
            {
                IsStart = false;
            });

        }

        /// 
        /// 窗体关闭事件
        /// 
        /// 
        /// 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 弹出提示框
            DialogResult result = MessageBox.Show("确定要关闭窗体吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (result == DialogResult.Yes)
            {
                // 关闭窗体
                e.Cancel = false;
            }
            else
            {
                // 不关闭窗体
                e.Cancel = true;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        /// 
        /// 计数清零
        /// 
        /// 
        /// 
        private void button5_Click(object sender, EventArgs e)
        {
            if (IsStart)
            {
                MessageBox.Show("程序已经启动!");
                return;
            }
            this.richTextBox1.Text = "计数器:0";
            this.richTextBox2.Text = "计数器:0";
            this.richTextBox3.Text = "计数器:0";
            this.richTextBox4.Text = "计数器:0";
        }
    }

你可能感兴趣的:(工业互联网(WPF),c#,开发语言)