zoukankan      html  css  js  c++  java
  • C#线程暂停和继续操作

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace XCZT
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Label.CheckForIllegalCrossThreadCalls = false;
            }
            
            ManualResetEvent ma = new ManualResetEvent(false);
    
            bool stop = false;
    
            //启动
            private void button1_Click(object sender, EventArgs e)
            {
                Thread thread = new Thread(Runtime);
    
                stop = false;
    
                ma.Set();// 信号打开,不阻塞当前线程
    
                thread.Start();
            }
    
            /// <summary>
            /// 线程
            /// </summary>
            void Runtime()
            {
                for (int i = 1; i <= 100; i++)
                {
                    if (stop)
                    {
                        return;
                    }
                        
                    ma.WaitOne();//根据是否收到信号判断是否阻塞当前线程
    
                    textBox1.AppendText("计时 :" + i + "
    ");
    
                    Thread.Sleep(100);
                }
            }
    
            //暂停
            private void button2_Click(object sender, EventArgs e)
            {
                ma.Reset();//信号关闭阻塞当前线程
    
                textBox1.AppendText("暂停中 :
    ");
            }
    
            //继续
            private void button3_Click(object sender, EventArgs e)
            {
    
                ma.Set();//信号打开,不阻塞当前线程
    
                textBox1.AppendText("继续计时 :
    ");
            }
    
            //停止
            private void button4_Click(object sender, EventArgs e)
            {
                stop = true;
    
                textBox1.AppendText("停止计时 
    ");
            }
        }
    }

  • 相关阅读:
    1602液晶显示实验
    LNMP安装(二)
    LNMP安装(一)
    vim插件安装
    资料下载
    建表的sql
    time
    计算机里的加减乘除
    branch
    存储过程
  • 原文地址:https://www.cnblogs.com/LcVong/p/12651599.html
Copyright © 2011-2022 走看看