线程里的 Thread + AutoResetEvent 请教啥意思?

色郎中 2006-11-06 03:35:28
发送和接收的多线程处理,建议采取Thread加上 AutoResetEvent来控制。这一部分如果无法完成可以来到北京后再做

这是MS 的专家说的 没明白 AutoResetEvent 是啥意思

哪位知道的 说说, 最好有点代码事例 谢谢
...全文
204 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
色郎中 2006-11-08
  • 打赏
  • 举报
回复
再顶下
色郎中 2006-11-08
  • 打赏
  • 举报
回复
还顶
SZSEAWIND 2006-11-08
  • 打赏
  • 举报
回复
顶瓜你!!!!!!!!!!!!
色郎中 2006-11-07
  • 打赏
  • 举报
回复
TKS
haplake 2006-11-07
  • 打赏
  • 举报
回复
AutoResetEvent类:通知正在等待的线程已发生事件,允许线程通过发信号互相通信。通常,此通信涉及线程需要独占访问的资源。


class MyMainClass
{
//Initially not signaled.
const int numIterations = 100;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static int number;

static void Main()
{
//Create and start the reader thread.
Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start();

for(int i = 1; i <= numIterations; i++)
{
Console.WriteLine("Writer thread writing value: {0}", i);
number = i;

//Signal that a value has been written.
myResetEvent.Set();

//Give the Reader thread an opportunity to act.
Thread.Sleep(0);
}

//Terminate the reader thread.
myReaderThread.Abort();
}

static void MyReadThreadProc()
{
while(true)
{
//The value will not be read until the writer has written
// at least once since the last read.
myResetEvent.WaitOne();
Console.WriteLine("{0} reading value: {1}", Thread.CurrentThread.Name, number);
}
}
}
色郎中 2006-11-07
  • 打赏
  • 举报
回复
没人顶撒
色郎中 2006-11-06
  • 打赏
  • 举报
回复
自己顶下

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧