• 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("停止计时 
    ");
            }
        }
    }

  • 相关阅读:
    IOS中多版本,多设备类型支持注意事项
    runtime
    网络搜集各种iOS开源类库
    Foudation框架常用结构体和常用类
    iOS开发-正则表达式的使用方法
    Github上的iOS资料-个人记录
    iOS应用程序多语言本地化
    星期判断
    【设定本地通知为周一到周五提醒, 周末不提醒解决办法】
    iOS9 HTTPS解决办法
  • 原文地址:https://www.cnblogs.com/LcVong/p/12651599.html
Copyright © 2020-2023  润新知