Thread
Thread t1 = new Thread(()=> {
MessageBox.Show("t1任务");
// 线程暂停1秒
Thread.Sleep(1000);
});
// 设置线程优先级
t1.Priority = ThreadPriority.Normal;
// 开始线程
t1.Start();
// 带参数线程
Thread t2 = new Thread((num) => { MessageBox.Show($"传入的参数:{num}"); });
// 传递参数
t2.Start(10);
// 中止线程
//t2.Abort();
ThreadPool
// 设置最大线程数
ThreadPool.SetMaxThreads(5,5);
int money = 0;
for (int i = 0; i < 60; i++)
{
// 第一个将线程添加到线程池中,第一个参数是callback事件,第二个参数是传入的参数
ThreadPool.QueueUserWorkItem(o=> {
money += Convert.ToInt32(o);
Thread.Sleep(500);
this.Invoke(new Action(() => { this.moneyLable.Text = money.ToString(); }));
},5000);
}
Tasks
TaskFactory
// TaskFa