Rx extensions: Where is Parallel.ForEach?

前端 未结 2 1709
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 17:39

I have a piece of code which is using Parallel.ForEach, probably based on a old version of Rx extensions or the Tasks Parallel Library. I installed a current ve

相关标签:
2条回答
  • 2020-12-31 18:08

    No need to do all this silly goosery if you have Rx:

    records.ToObservable()
        .SelectMany(x => Observable.Start(() => ProcessRecord(x), Scheduler.ThreadPoolScheduler))
        .ToList()
        .First();
    

    (Or, if you want the order of the items maintained at the cost of efficiency):

    records.ToObservable()
        .Select(x => Observable.Start(() => ProcessRecord(x), Scheduler.ThreadPoolScheduler))
        .Concat()
        .ToList()
        .First();
    

    Or if you want to limit how many items at the same time:

    records.ToObservable()
        .Select(x => Observable.Defer(() => Observable.Start(() => ProcessRecord(x), Scheduler.ThreadPoolScheduler)))
        .Merge(5 /* at a time */)
        .ToList()
        .First();
    
    0 讨论(0)
  • 2020-12-31 18:28

    Here's a simple replacement:

    class Parallel
    {
        public static void ForEach<T>(IEnumerable<T> source, Action<T> body)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (body == null)
            {
                throw new ArgumentNullException("body");
            }
            var items = new List<T>(source);
            var countdown = new CountdownEvent(items.Count);
            WaitCallback callback = state =>
            {
                try
                {
                    body((T)state);
                }
                finally
                {
                    countdown.Signal();
                }
            };
            foreach (var item in items)
            {
                ThreadPool.QueueUserWorkItem(callback, item);
            }
            countdown.Wait();
        }
    }
    
    0 讨论(0)
提交回复
热议问题