Async Queue Processing With Reactive Extensions

后端 未结 2 994
别那么骄傲
别那么骄傲 2021-02-09 07:27

There are a couple of articles on this, and I have this working...but I want to know how to set a max number of Task threads for my Observable subscriptions at once.

I h

2条回答
  •  野性不改
    2021-02-09 08:07

    If you create your "work" as IObservable with deferred execution (ie. they want do anything until subscribed to), you can use the Merge overload that accepts a number of maximum concurrent subscriptions:

    ISubject synchronizedQueue = new Subject().Synchronize();
    
    queue
        .Select(item => StartWork(item))
        .Merge(maxConcurrent: 5) // C# 4 syntax for illustrative purposes
        .Subscribe();
    
    // To enqueue:
    synchronizedQueue.OnNext(new QueueItem());
    

提交回复
热议问题