① 线程中的指令:一个方法(委托)
② 线程中的数据:相关的对象;
public Thread(ThreadStart fun);
public delegate void ThreadStart();
① Thread thread=
new Thread(new ThreadStart(obj.fun));
② thread.Start();
③ 有时,使用匿名函数及Lambda表达式更方便;
线程函数会一直执行下去,直至它结束
Abort()终止;
Suspend()挂起 Resume()恢复
Sleep(毫秒数)
将单独的执行线程合并成一个线程;
Lock(对象或表达式)
{
…语句;
}
System.Threading.Monitor.Enter(对象或表达式);
try
{
…
}
finally
{
System.Threadiing.Monitor.Exit(对象或表达式);
}
Threadpool.QueueUserWorkItem()等方法来提交相应的任务;
QueueUserWorkItem(WaitCallback,object)
QueueUserWorkItem(WaitCallback)
其中public delegate void WaitCallback(objec state);
Timer的构造方法如下:
public Timer(TimerCallback callback,//执行的任务
object state,//数据
int dueTime, //启动前的延时
int period //任务之间的间隔
);
其中TimerCallback是:
public delegate void TimerCallback(object state);
直接从工具箱托过来
属性 Interval,Enabled
事件 Tick
① IsSynchoronized 属性用于判断是否为同步版本,SyncRoot属性提供了集合自己的同步版本;
② Array,ArrayList,SortedList,Hashtable等,都可以使用Synchronized()方法获取一个线程安全的包装对象;
if(this.InvokeRequired)
{
this.BeginInvoke(new AddMsg(this.AddMsgFun),new object[]{msg});//显示到界面
}
else
{
this.AddMsgFun(msg);
}
① 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);
}
}
}
}