C# 线程池执行操作例子
public partial class Form1 : Form
{
CountdownEvent hander = new CountdownEvent(10);
public static object lock_action = new object();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Run1), i);
}
textBox1.Invoke(new Action(() =>
{
textBox1.AppendText("按钮1执行完毕".Log());
}));
}
private void button2_Click(object sender, EventArgs e)
{
//ThreadPool.SetMaxThreads(10, 30);
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Run2), i);
}
hander.Wait();
textBox1.AppendText("按钮2执行完毕".Log());
}
private void Run1(object str)
{
Thread.Sleep(1000);
textBox1.AppendText(str.ToString().Log());
}
private void Run2(object str)
{
Thread.Sleep(1000);
textBox1.AppendText(str.ToString().Log());
hander.Signal();
}
}