AutoResetEvent 仅在未设置该次数时才等待的最佳方法
我可能超出了 AutoResetEvent 的正确设计,但不太知道该求助于什么。我想要这种行为:
var autoResetEvent = new AutoResetEvent(false);
autoResetEvent.Set();
autoResetEvent.Set();
autoResetEvent.WaitOne();
// since Set was called twice, I don't want to wait here
autoResetEvent.WaitOne();
不幸的是(对于我的问题),这不是 AutoResetEvent 的行为方式。对于这种情况,最适合使用什么类?
注意:我可以访问 .NET 并行扩展库。
I'm probably going outside of the proper design for an AutoResetEvent but don't quite know what to turn to. I want this behavior:
var autoResetEvent = new AutoResetEvent(false);
autoResetEvent.Set();
autoResetEvent.Set();
autoResetEvent.WaitOne();
// since Set was called twice, I don't want to wait here
autoResetEvent.WaitOne();
Unfortunately (for my problem), this is not how an AutoResetEvent behaves. What is the best class to use for this situation?
Note: I have access to the .NET Parallel Exensions library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否正在寻找信号量?
Are you looking for a Semaphore?
如果您有能力使用并行扩展库,那么您应该使用它。调用工作任务然后等待它们完成执行是很简单的。
这应该产生以下输出:
有关 .Net 中线程(包括 TPL)的更多阅读,您应该检查出这个特殊的资源:http://www.albahari.com/threading/
If you have the ability to use the Parallel Extensions Library then you should use that instead. It is trivial to invoke worker tasks and then wait on them to finish executing.
This should produce the following output:
For more reading on threading in .Net including the TPL, you should check out this exceptional resource: http://www.albahari.com/threading/