用ManualResetEvent和AutoResetEvent可以很好的控制线程的运行和线程之间的通信。msdn的参考为:
http://msdn.microsoft.com/zh-cn/library/system.threading.autoresetevent.aspx
http://msdn.microsoft.com/zh-cn/library/system.threading.manualresetevent.aspx下面我写个例子,这里模拟了一个线程更新数据,两个线程读取数据。更新的时候需要阻止读取的两个现成工作。而另外还有一个信号量来控制线程的退出。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication35
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} System.Threading.ManualResetEvent mEvent = new System.Threading.ManualResetEvent(true); //判断线程安全退出的信号量
System.Threading.ManualResetEvent mEventStopAll = new System.Threading.ManualResetEvent(false); //*******ManualResetEvent的用法。
private void button1_Click(object sender, EventArgs e)
{
//一个线程模拟写入
new System.Threading.Thread(invokeWrite).Start();
//两个线程模拟读取
new System.Threading.Thread(invokeRead).Start();
new System.Threading.Thread(invokeRead).Start();
} private void invokeWrite()
{
for (int i = 0; i < 100; i++)
{
//判断线程安全退出
if (mEventStopAll.WaitOne(10, false) == true) break;
//设置信号量,假设更新数据需要2秒,每更新一次暂停2秒.
mEvent.Reset();
Console.WriteLine("正在更新...");
System.Threading.Thread.Sleep(2000);
mEvent.Set();
System.Threading.Thread.Sleep(2000);
}
}
private void invokeRead()
{
while (mEvent.WaitOne() == true)
{
//判断线程安全退出
if (mEventStopAll.WaitOne(10, false) == true) break;
//假设读取一体数据用10毫秒.他需要判断信号量开关.
Console.WriteLine("读取一条数据:");
System.Threading.Thread.Sleep(10);
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
mEventStopAll.Set();
}
}
}那么ManualResetEvent和AutoResetEvent的区别是什么,这个看名字就知道了,AutoResetEvent在WaitOne之后会自动将信号量关闭,也就是说它一次只能唤醒一个线程。
------------------------
很多人发蛋贴攒分,我也发发,但是有1分,发了也不能白发,我会随口说个技术基础点,不只局限于代码。
高手可以忽略本贴
http://msdn.microsoft.com/zh-cn/library/system.threading.autoresetevent.aspx
http://msdn.microsoft.com/zh-cn/library/system.threading.manualresetevent.aspx下面我写个例子,这里模拟了一个线程更新数据,两个线程读取数据。更新的时候需要阻止读取的两个现成工作。而另外还有一个信号量来控制线程的退出。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication35
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} System.Threading.ManualResetEvent mEvent = new System.Threading.ManualResetEvent(true); //判断线程安全退出的信号量
System.Threading.ManualResetEvent mEventStopAll = new System.Threading.ManualResetEvent(false); //*******ManualResetEvent的用法。
private void button1_Click(object sender, EventArgs e)
{
//一个线程模拟写入
new System.Threading.Thread(invokeWrite).Start();
//两个线程模拟读取
new System.Threading.Thread(invokeRead).Start();
new System.Threading.Thread(invokeRead).Start();
} private void invokeWrite()
{
for (int i = 0; i < 100; i++)
{
//判断线程安全退出
if (mEventStopAll.WaitOne(10, false) == true) break;
//设置信号量,假设更新数据需要2秒,每更新一次暂停2秒.
mEvent.Reset();
Console.WriteLine("正在更新...");
System.Threading.Thread.Sleep(2000);
mEvent.Set();
System.Threading.Thread.Sleep(2000);
}
}
private void invokeRead()
{
while (mEvent.WaitOne() == true)
{
//判断线程安全退出
if (mEventStopAll.WaitOne(10, false) == true) break;
//假设读取一体数据用10毫秒.他需要判断信号量开关.
Console.WriteLine("读取一条数据:");
System.Threading.Thread.Sleep(10);
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
mEventStopAll.Set();
}
}
}那么ManualResetEvent和AutoResetEvent的区别是什么,这个看名字就知道了,AutoResetEvent在WaitOne之后会自动将信号量关闭,也就是说它一次只能唤醒一个线程。
------------------------
很多人发蛋贴攒分,我也发发,但是有1分,发了也不能白发,我会随口说个技术基础点,不只局限于代码。
高手可以忽略本贴
解决方案 »
- ContexMenuStrip右键菜单
- 关于Linq 动态拼接查询语句问题
- C# AutoCAD程序老是崩溃,高手请进。帮帮我!
- hibernate问题:Key being added: hibernate.show_sql
- 静态方法的好处是什么?
- 将XML添加到treeview ?另有两帖每贴20分 答出一并给了~
- 增加xml文件记录的问题,节点丢失了?????
- ftp主动模式文件上传源代码询问
- VS2010+.net4.0仿照苹果手机上的消除之星写了一个电脑版的功能简单 稍后将公开源码
- 我急了!(放100分)
- C#.net连接远程数据库问题
- TextBox的问题判断赋值问题..有没有简单的方法?
PS:上次看到你孩子,蛮可爱的!