http://msdn.microsoft.com/en-us/library/tdykks7z.aspx
根据文档,返回值为:
“满足等待的对象的数组索引。”
因此,这意味着索引表示已设置的事件,并且此代码将导致死锁,因为它将等待自己:
private static AutoResetEvent waitLock()
{
//Wait for any of the events to be signaled
AutoResetEvent autoEvent;
lock(yahooRequests) //Note: yahoo requests is a array of auto reset events
{
int index = AutoResetEvent.WaitAny(yahooRequests);
autoEvent = yahooRequests[index];
autoEvent.WaitOne();
}
return autoEvent;
}
这个代码是正确的:
private static AutoResetEvent waitLock()
{
//waitany returns the index of a successfull wait. So this line returns the reference to a autoresetevent.
return yahooRequests[AutoResetEvent.WaitAny(yahooRequests)];
}
我只是想确保(在我看来)文件不是100%清楚
编辑:
我的设计有缺陷——正如@Hans Passant所指出的那样,我本应该使用信号灯。因为我想确保有N个yahooRequest可以访问一个函数。但@arno从技术上回答了最初的问题。真希望我能设置两个可接受的asnwer
编辑:
正如@Sriram Sakthvel在评论中指出的那样,第一个例子将永远等待自己。但实际上并不是僵局。