的AutoResetEvent澄清 [英] AutoResetEvent clarification
问题描述
我要说明的是如何将以下代码works.I已开列出了我的怀疑得到您的答复。
类AutoResetEventDemo
{
静态的AutoResetEvent autoEvent =新的AutoResetEvent(假);
静态无效的主要()
{
Console.WriteLine(...主要出发......);
ThreadPool.QueueUserWorkItem
(新WaitCallback(CodingInCSharp),autoEvent);
如果(autoEvent.WaitOne(1000,FALSE))
{
Console.WriteLine(编码singalled(编码完成));
}
,否则
{
Console.WriteLine(超时等待编码);
}
Console.WriteLine(..主营结束...);
Console.ReadKey(真);
}
静态无效CodingInCSharp(对象将stateInfo)
{
Console.WriteLine(开始编码);
Thread.sleep代码(新的随机()和Next(100,2000)。);
Console.WriteLine(编码过);
((的AutoResetEvent)将stateInfo)。设置();
}
}
-
静态的AutoResetEvent autoEvent =新的AutoResetEvent(假);
在初始阶段,信号被设置为false
。 -
ThreadPool.QueueUserWorkItem(新WaitCallback(CodingInCSharp),autoEvent);
选择从线程池线程,使该线程执行CodingInCSharp。
WaitCallback的目的是main()的线程
完成它的执行。以后要执行的方法 -
autoEvent.WaitOne(1000,FALSE)
等待1秒,从CodingInCSharp)
得到信号柜面如果我使用WaitOne的(1000,真正),将它杀死它从
线程池收到的线程? -
如果我这样做没有设置
((的AutoResetEvent)将stateInfo)。设置();
将在main()无限期地等待信号
该WaitCallback在同时到Main方法,只要一个线程池线程变得可用执行。
Main方法等待线程池线程上CodingInCSharp方法来设置信号1秒。如果信号设为1秒内,主方法打印编码singalled(编码完成)
。如果信号没有在1秒内设置的主要方法将中止等待信号和印刷超时等待编码
。在这两种情况下,主要方法进入等待按下一个键。
设置信号或达到超时不杀一个线程。
的主要方法不会无限期地等待,如果信号没有被设置,因为等待信号,如果信号没有在1秒内设置被中止。
I want to clarify how the following code works.I have itemized my doubts to get your reply.
class AutoResetEventDemo
{
static AutoResetEvent autoEvent = new AutoResetEvent(false);
static void Main()
{
Console.WriteLine("...Main starting...");
ThreadPool.QueueUserWorkItem
(new WaitCallback(CodingInCSharp), autoEvent);
if(autoEvent.WaitOne(1000, false))
{
Console.WriteLine("Coding singalled(coding finished)");
}
else
{
Console.WriteLine("Timed out waiting for coding");
}
Console.WriteLine("..Main ending...");
Console.ReadKey(true);
}
static void CodingInCSharp(object stateInfo)
{
Console.WriteLine("Coding Begins.");
Thread.Sleep(new Random().Next(100, 2000));
Console.WriteLine("Coding Over");
((AutoResetEvent)stateInfo).Set();
}
}
static AutoResetEvent autoEvent = new AutoResetEvent(false);
At initial stage signal is set to false.
ThreadPool.QueueUserWorkItem(new WaitCallback(CodingInCSharp), autoEvent);
Select a thread from ThreadPool and make that thread to execute CodingInCSharp. The purpose of WaitCallback is to execute the method after Main() thread finishes its execution.
autoEvent.WaitOne(1000,false)
Wait for 1 sec to get the signal from "CodingInCSharp") Incase if i use WaitOne(1000,true), will it kill the thread it received from threadpool?
If I did not set
((AutoResetEvent)stateInfo).Set();
will the Main() indefinitely wait for signal?
The WaitCallback is executed in concurrently to the Main method as soon as a threadpool thread becomes available.
The Main method waits 1 second for the CodingInCSharp method on the threadpool thread to set the signal. If the signal is set within 1 second, the Main method prints "Coding singalled(coding finished)"
. If the signal is not set within 1 second, the Main method aborts waiting for the signal and prints "Timed out waiting for coding"
. In both cases the Main method proceeds to wait for a key to be pressed.
Setting a signal or reaching a timeout does not "kill" a thread.
The Main method will not wait indefinitely if the signal is not set, because waiting for the signal is aborted if the signal is not set within 1 second.
这篇关于的AutoResetEvent澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!