zoukankan      html  css  js  c++  java
  • 线程暂停与继续

    这篇文章主要是答复百度网友的问题的,代码做了注释,就不解释了。线程的暂停,我使用了WaitOne方法,线程的继续,则使用了set方法,其实,这两种方法都存在于AutoResetEvent类中,这个类主要是通过设置信号量来实现线程的暂停与继续的。

    代码如下:

    using System;
    using System.Windows.Forms;
    using System.Threading;
     
    namespace ThreadInUI
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                for (int i = 0; i < 11; i++)
                {
                    a[i] = new AutoResetEvent(false);  //初始化
                    b[i] = new AutoResetEvent(false);
                }
            }
     
            string[] threadID = new string[11];
            AutoResetEvent[] a = new AutoResetEvent[11];
            AutoResetEvent[] b = new AutoResetEvent[11];
            private void btnStart_Click(object sender, EventArgs e)
            {
                lblLog.Invoke((Action)(() => { lblLog.Text += "
    当前主线程ID为:" + Thread.CurrentThread.ManagedThreadId.ToString(); }));
                for (int i = 0; i < 10; i++)
                {
                    int j = i;  //这个用于避免闭包
                    Thread t = new Thread(new ThreadStart(() =>
                    {
                       a[j].WaitOne();  //这个用于获取子线程的ID
                        lblLog.Invoke((Action)(() => { lblLog.Text += "
    线程"+threadID[j]+"睡眠3s"; }));
                        Thread.Sleep(3000);
                        lblLog.Invoke((Action)(() => { lblLog.Text += "
    线程" + threadID[j] + "暂停"; }));
                        b[j].WaitOne(); //这个用于暂停所有子线程的运作
                        lblLog.Invoke((Action)(() => { lblLog.Text += "
    ===线程"+threadID[j]+"恢复==="; }));
                    }));
                    threadID[i] = t.ManagedThreadId.ToString();
                    a[j].Set(); //当子线程的ID生成后,这里放行,目的是避免取不到Thread的ID
                    t.Start();
                }
            }
     
            private void btnGoOn_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    b[i].Set();   //放行所有的子线程
                }
            }
        }
    }

    实现的效果如下:

    当点击“打开10个线程并阻塞”按钮的时候:

    QQ截图20131011160602

    当点击“继续运行10个线程”按钮的时候:

    QQ截图20131011160617

    代码很简单,源码我就不加上了。

  • 相关阅读:
    python中int类型、bool类型补充,及字符串的部分常用方法
    while循环、流程控制、基本运算符、编码问题、in和not in
    python的起源、解释器、变量用户交互、流程控制
    Spring-Security (学习记录三)--读取数据库中的用户和角色
    Spring-Security (学习记录二)--修改为自己的登录页面
    java中get请求的中文乱码问题
    python打开文件的方式
    js闭包
    Java内部类的整理。
    HTML+CSS标签的属性及应用
  • 原文地址:https://www.cnblogs.com/scy251147/p/3363667.html
Copyright © 2011-2022 走看看