一 多线程的概念
1 进程Process
2 线程Thread
① 线程中的指令:一个方法(委托)
② 线程中的数据:相关的对象;
3 System.Threading.Thread属性
4 System.Threading.Thread方法
5 线程的创建
1) Thread类有一个构造方法,格式如下:
public Thread(ThreadStart fun);
2) 其中ThreadStart是一个委托
public delegate void ThreadStart();
3) 下面是创建一个Thread对象并启动这个线程的一般方法:
① Thread thread=
new Thread(new ThreadStart(obj.fun));
② thread.Start();
③ 有时,使用匿名函数及Lambda表达式更方便;
6 线程的启动和停止
1) 启动:调用线程对象的Start()
2) 停止
线程函数会一直执行下去,直至它结束
3) 另外
Abort()终止;
Suspend()挂起 Resume()恢复
Sleep(毫秒数)
4) 线程的状态 ThreadState
5)线程的优先级ThreadPriority
二 线程的同步
1 使用Join()方法
将单独的执行线程合并成一个线程;
2 Lock语句与Monitor类
Lock(对象或表达式)
{
…语句;
}
System.Threading.Monitor.Enter(对象或表达式);
try
{
…
}
finally
{
System.Threadiing.Monitor.Exit(对象或表达式);
}
3 用于同步控制的类
4 线程池及其相关类
Threadpool.QueueUserWorkItem()等方法来提交相应的任务;
QueueUserWorkItem(WaitCallback,object)
QueueUserWorkItem(WaitCallback)
其中public delegate void WaitCallback(objec state);
5 System.Threading.TImer类
Timer的构造方法如下:
public Timer(TimerCallback callback,//执行的任务
object state,//数据
int dueTime, //启动前的延时
int period //任务之间的间隔
);
其中TimerCallback是:
public delegate void TimerCallback(object state);
6 System.Windows.Forms.Timer类
直接从工具箱托过来
属性 Interval,Enabled
事件 Tick
三 集合的线程安全性
① IsSynchoronized 属性用于判断是否为同步版本,SyncRoot属性提供了集合自己的同步版本;
② Array,ArrayList,SortedList,Hashtable等,都可以使用Synchronized()方法获取一个线程安全的包装对象;
四 Windows界面与线程
1 界面的主线程
2 对界面的更新智能使用主线程
3 其他线程则可以这样
if(this.InvokeRequired)
{
this.BeginInvoke(new AddMsg(this.AddMsgFun),new object[]{msg});//显示到界面
}
else
{
this.AddMsgFun(msg);
}
五 使用BackgroundWorker组件
① DoWork事件;
② RunWokerAsync方法;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace 线程的Join
{
internal class Program
{
static void Main(string[] args)
{
Runner r = new Runner();
Thread thread = new Thread(r.run);
thread.Start();
thread.Join();
for(int i=0;i<10;i++)
{
Console.WriteLine("t" + i);
Thread.Sleep(100);
}
}
}
class Runner
{
public void run()
{
for(int i=0;i<10;i++)
{
Console.WriteLine(i);
Thread.Sleep(100);
}
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 多线程绘图
{
public partial class Form1 : Form
{
private ArrayList threads = new ArrayList();
public Form1()
{
InitializeComponent();
}
void AddMovingObject()
{
MovingShape obj = new MovingShape(this.pictureBox1);
Thread thread = new Thread(new ThreadStart(obj.run));
thread.IsBackground = true;
thread.Start();
threads.Add(thread);
}
private void Form1_Load(object sender, EventArgs e)
{
AddMovingObject();
}
private void button1_Click(object sender, EventArgs e)
{
AddMovingObject();
}
private void button2_Click(object sender, EventArgs e)
{
foreach(Thread thread in threads)
{
thread.Suspend();
}
}
private void button3_Click(object sender, EventArgs e)
{
foreach(Thread thread in threads)
{
thread.Resume();
}
}
}
public class MovingShape
{
bool bContinue = false;
private int size = 60;
private int speed = 10;
private Color color;
private Brush brush;
private Pen pen;
private int type;
private int x, y, w, h, dx, dy;
protected Control app;
Random rnd = new Random();
public MovingShape(Control app)
{
this.app = app;
x = rnd.Next(app.Width);
y = rnd.Next(app.Height);
w = rnd.Next(size);
h = rnd.Next(size);
dx = rnd.Next(speed);
dy = rnd.Next(speed);
color = Color.FromArgb(
rnd.Next(128, 255),
rnd.Next(128, 255),
rnd.Next(128, 255));
pen = Pens.Black;
brush = new SolidBrush(color);
type = rnd.Next(3);
bContinue = true;
}
public void run()
{
while(true)
{
x += dx;
y += dy;
if (x < 0 || x + w > app.Width)
dx = -dx;
if (y < 0 || y + h > app.Height)
dy = -dy;
Graphics g = app.CreateGraphics();
switch(type)
{
case 0:
g.FillRectangle(brush, x, y, w, h);
g.DrawRectangle(pen, x, y, w, h);
break;
case 1:
g.FillEllipse(brush, x, y, w, h);
g.DrawEllipse(pen, x, y, w, h);
break;
case 2:
g.FillPie(brush, x, y, w, h, 0.1F, 0.9F);
g.DrawArc(pen, x, y, w, h, 0.1F, 0.9F);
break;
}
Thread.Sleep(130);
}
}
}
}