private ConcurrentQueue<Data> _queue = new ConcurrentQueue<Data>();
private AutoResetEvent _queueNotifier = new AutoResetEvent(false);
public void MoreData(Data example)
{
_queue.Enqueue(example);
_queueNotifier.Set();
}
private void _SimpleThreadWorker()
{
while (_socket.Connected)
{
_queueNotifier.WaitOne();
Data data;
if (_queue.TryDequeue(out data))
{
//handle the data
}
}
}
当我拥有它时,我是否必须将该事件设置为假当它返回_queueNotifier.WaitOne()
或它是如何工作的时候,出列还是事件回到假?AutoResetEvent进程?
我是否应该像下面的例子那样使用inner,或者两种方式都好吗?
while (_socket.Connected)
{
_queueNotifier.WaitOne();
while (!_queue.IsEmpty)
{
Data data;
if (_queue.TryDequeue(out data))
{
//handle the data
}
}
}
我会检查一下,但我仍然有兴趣了解AutoResetEvent如何在未来再次需要时进一步了解它。 – Prix
@Prix:已编辑以包含该信息。有关更多信息,请参阅AutoResetEvent的文档。 –
感谢您的更新,至于你刚才说的话,我有点困惑,因为将concurrentqueue包装在blockingcollection中,而不是仅仅使用blockingcolletion更容易?如果你能,我会欣赏一些样本,所以我可以更好地理解你在说什么...... – Prix