我有以下异步队列处理路由。无功扩展等待队列空
var commandQueue = new BlockingCollection<MyCommand>();
commandQueue
.GetConsumingEnumerable()
.ToObservable(new LimitedConcurrencyLevelTaskPoolScheduler(5))
.Subscribe(c =>
{
try
{
ProcessCommand(c);
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
}
}
);
在一个特定的情况下(当我即将得到一些数据),我需要确保我commandQueue是前走出去,让数据为空。预计此操作将同步发生。基本上,我想这样做
public void GetData()
{
commandQueue.WaitForEmpty();
// could potentially be expressed:
// while (commandQueue.Count > 0) Thread.Sleep(10);
return GoGetTheData()
}
我意识到,在理想情况下,所有的来电者“的GetData”异步......但有时它的必要,它发生在一个同步的方式...等我需要等待命令队列为空以确保数据的一致性和最新性。
我知道我如何使用ManualResetEvent轻松完成此操作...但我想知道System.Reactive/TPL是否有简单的方法。
谢谢。
问题是,没有这样的方法WaitForEmpty – Jeff
@ JeffN825 - 什么是BlockCollection呢?这是你定义的东西吗? – Enigmativity
对不起,我不确定在我的例子中如何被截断。它是BlockingCollection http://msdn.microsoft.com/en-us/library/dd267312.aspx – Jeff