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

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

  • 相关阅读:
    C++Builder中的异常传递
    lpc1343 usb isp not work in linux and mac
    玩玩Hiweed linux 2.0
    有关 stringWithString 和 initWithString
    Windows mobile 中获取内存使用情况
    玩玩xubuntu 8.10
    升级我的ipaq hx2110到Windows Mobile 6.0
    面试技巧 from IBM
    常用Sql语句
    c#的事件机制示例代码: 猫> 老鼠, 主人
  • 原文地址:https://www.cnblogs.com/scy251147/p/3363667.html
Copyright © 2011-2022 走看看