在不旋转的情况下保持任务活动和所有订阅活动的正确方法是什么?

发布于 2025-01-08 10:13:11 字数 808 浏览 6 评论 0原文

我在聚合方法实现中有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2025-01-15 10:13:11

等待取消令牌的等待句柄:

while ( !token.IsCancellationRequested )
{
    if ( token.WaitHandle.WaitOne( timeout ) )
    {
        // cancelled. Don't have to do anything here, the above while 
        // loop will break now.
    }
    else
    {
        // ''timeout' period elapsed - do some periodic work here.
    }

}

编辑:
如果您没有在此线程上执行任何定期工作,则只需使用不带超时参数的 WaitOne 重载即可。

token.WaitHandle.WaitOne();

这将无限期地等待,直到发出取消令牌信号,然后继续。

编辑2:

我刚刚读到您说您在可观察的订阅中有 while 循环。它应该是在您设置所有可观察量订阅之后,但不是在每个实际订阅回调中(这些订阅将在调用源事件的任何线程或可能的其他线程池线程上运行,而不是在设置订阅的任务线程上运行) )。

wait on the cancellation token's wait handle:

while ( !token.IsCancellationRequested )
{
    if ( token.WaitHandle.WaitOne( timeout ) )
    {
        // cancelled. Don't have to do anything here, the above while 
        // loop will break now.
    }
    else
    {
        // ''timeout' period elapsed - do some periodic work here.
    }

}

EDIT:
if you don't have any periodic work to do on this thread, then simply use the WaitOne overload without a timeout parameter.

token.WaitHandle.WaitOne();

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文