public void Start(IEnumerable <int> inputValues)
        {
            var lockObject      = new object();
            var aggregateResult = new BatchResult();

            Parallel.ForEach(inputValues, item => {
                try {
                    HardRemoteWorker.HardWork(item);
                } catch (Exception ex) {
                    lock (lockObject) {
                        aggregateResult.AddError(ex.Message);
                    }
                }
            });

            if (aggregateResult.Success == false)
            {
                Console.WriteLine(string.Join("\n", aggregateResult.Errors));
            }
        }
Exemple #2
0
        public void Start(IEnumerable <int> inputValues)
        {
            var lockObject      = new object();
            var aggregateResult = new BatchResult();

            var tasks = new List <Task>();

            //Parallel.ForEach(inputValues, item => {
            //    try {
            //        HardRemoteWorker.HardWork(item);
            //    } catch (Exception ex) {
            //        lock (lockObject) {
            //            aggregateResult.AddError(ex.Message);
            //        }
            //    }
            //});
            foreach (var value in inputValues)
            {
                var task = Task.Factory.StartNew(async() =>
                {
                    await HardRemoteWorker.HardWork(value);
                }).Unwrap();
                tasks.Add(task);
            }

            try
            {
                Task.WaitAll(tasks.ToArray());
            }
            catch (AggregateException e)
            {
                Console.WriteLine(e.ToString());
                //throw;
            }

            if (aggregateResult.Success == false)
            {
                Console.WriteLine(string.Join("\n", aggregateResult.Errors));
            }
        }