這篇文章主要是答復百度網友的問題的,代碼做了注釋,就不解釋了。線程的暫停,我使用了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 += "\r\n當前主線程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 += "\r\n線程"+threadID[j]+"睡眠3s"; }));
Thread.Sleep(3000);
lblLog.Invoke((Action)(() => { lblLog.Text += "\r\n線程" + threadID[j] + "暫停"; }));
b[j].WaitOne(); //這個用於暫停所有子線程的運作
lblLog.Invoke((Action)(() => { lblLog.Text += "\r\n===線程"+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個線程並阻塞”按鈕的時候:
當點擊“繼續運行10個線程”按鈕的時候:
代碼很簡單,源碼我就不加上了。