在不旋转的情况下保持任务活动和所有订阅活动的正确方法是什么?
我在聚合方法实现中有以下 TPL 任务
public abstract class AggregatorBase : IAggregator
{
public void Start(CancellationToken token)
{
var parent = Task.Factory.StartNew(x =>
{
Aggregate(token);
},TaskCreationOptions.LongRunning, token);
parent.Wait();
}
public abstract void Aggregate(CancellationToken ct);
}
我有许多 Observable.Subscription 以以下结尾
public override void Aggregate(CancellationToken ct)
{
this.observables.Subscribe(// Do stuff);
this.observables.Subscribe(// Do more stuff);
while (!token.IsCancellationRequested)
{
System.Threading.Thread.Sleep(1000)
}
}
问题是保持任务活动和所有订阅活动而不旋转的最佳方法是什么?
I have the following TPL Task
public abstract class AggregatorBase : IAggregator
{
public void Start(CancellationToken token)
{
var parent = Task.Factory.StartNew(x =>
{
Aggregate(token);
},TaskCreationOptions.LongRunning, token);
parent.Wait();
}
public abstract void Aggregate(CancellationToken ct);
}
within the Aggregate method implementations I have a number of Observable.Subscription's ending with the following
public override void Aggregate(CancellationToken ct)
{
this.observables.Subscribe(// Do stuff);
this.observables.Subscribe(// Do more stuff);
while (!token.IsCancellationRequested)
{
System.Threading.Thread.Sleep(1000)
}
}
Question is whats the best way of keeping the Task alive and all Subscriptions active without spinning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
等待取消令牌的等待句柄:
编辑:
如果您没有在此线程上执行任何定期工作,则只需使用不带超时参数的
WaitOne
重载即可。这将无限期地等待,直到发出取消令牌信号,然后继续。
编辑2:
我刚刚读到您说您在可观察的订阅中有 while 循环。它应该是在您设置所有可观察量订阅之后,但不是在每个实际订阅回调中(这些订阅将在调用源事件的任何线程或可能的其他线程池线程上运行,而不是在设置订阅的任务线程上运行) )。
wait on the cancellation token's wait handle:
EDIT:
if you don't have any periodic work to do on this thread, then simply use the
WaitOne
overload without a timeout parameter.that will wait indefinetly until the cancellation token is signalled, then continue.
EDIT2:
I just read that you said you had that while loop within the observable's subscriptions. It should be just after you have setup all your observables subscriptions, but not within each actual subscription callback (those subscriptions will run on whatever thread invoked the source event(s) or possibly other thread pool threads, not the task thread that setup the subscriptions).