我是否可以使用一种方法来设置IObservable.SelectMany的最大线程数?
以下代码非常适合在处理项目时更新UI,但我尝试执行的任务目前只是一点资源。我想将最大线程设置为两个,在资源使用上稍微轻一些。
AsyncCommand = ReactiveCommand.CreateAsyncObservable(_ =>
{
// Set progress bar indicator to 0
var set = new [] {...} // The set of items to process
// Set the progress bar indicator max to the count of the items to process
return set
.ToObservable()
.SelectMany((item, index) => Task.Run(() =>
{
// Process item
return item;
}), (item, index, processed) => item);
});
AsyncCommand
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(item =>
{
// Step the progress bar indicator
});
答案 0 :(得分:1)
Merge
有一个最大并行度参数:
AsyncCommand = ReactiveCommand.CreateAsyncObservable(_ =>
{
// Set progress bar indicator to 0
var set = new [] {...} // The set of items to process
// Set the progress bar indicator max to the count of the items to process
return set
.ToObservable()
.Select(item => Observable.FromAsync(() => DoAsyncProcess(item))))
.Merge(2);
});
答案 1 :(得分:0)