一、实例
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //允许调用当前线程下的控件
}
Thread T = null; //线程实例化
Thread T2 = null;
private void button1_Click(object sender, EventArgs e)
{
//第一步:多线程委托
ThreadStart TS = new ThreadStart(F);
ThreadStart TS2 = new ThreadStart(F2);
//第二步:多线程类
T = new Thread(TS);
T2 = new Thread(TS2);
//第三步:执行线程
T.Start();
T2.Start();
}
private void F() //方法的返回类型,参数必须与委托相同
{
for (int i = 0; i < 600; i++)
{
button2.Left = i;
}
}
private void F2() //方法的返回类型,参数必须与委托相同
{
for (int i = 0; i < 600; i++)
{
button3.Left = i;
}
}
// 关闭窗体
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
T.Abort(); //终止线程
T2.Abort();
}
}
}
二、线程的优先级别
T.Priority = ThreadPriority.Lowest; //老大低
T2.Priority = ThreadPriority.Highest; //老二优先级别高
三、线程的暂停和恢复
Thread.Sleep(10); //线程阻止10毫秒
T.Suspend(); //线程暂停
T.Resume(); //线程恢复