C#多线程中WaitOne函数的?

来源:互联网 发布:centos输入法下载安装 编辑:程序博客网 时间:2024/06/05 16:02


C#多线程中WaitOne函数的问题,请指教!


 函数
 

专业回答


dragon2snow
 2009-10-23 22:38

官方有代码,有说明,MSDN,有例子,为什么不看呢

using System;

using System.Threading;

namespace testmono

{

class WaitOne

 {

 static AutoResetEvent autoEvent = new AutoResetEvent (false);

static void Main ()

 {

 Console.WriteLine ("Main starting.");

ThreadPool.QueueUserWorkItem (new WaitCallback (WorkMethod), autoEvent);

// Wait for work method to signal.

 if (autoEvent.WaitOne (1000, false)) {

 Console.WriteLine ("Work method signaled.");

 } else {

 Console.WriteLine ("Timed out waiting for work " + "method to signal.");

 }

 Console.WriteLine ("Main ending.");

 }

static void WorkMethod (object stateInfo)

 {

 Console.WriteLine ("Work starting.");

// Simulate time spent working.

 Thread.Sleep (new Random ().Next (100, 2000));

// Signal that work is finished.

 Console.WriteLine ("Work ending.");

 ((AutoResetEvent)stateInfo).Set ();

 }

 }

你修改autoEvent.WaitOne (1000, false))
这个你就明白了,



分享 
 
本回答由科学教育分类达人 顾凤祥认证
 

其他类似问题

2009-06-09 C#多线程函数如何传参数4  
2010-11-20 请问用C语言在windows上建立多线程需要用什么函数最好,14  
2012-02-04 (C++多线程问题)CreateThread()?


0 0
原创粉丝点击