Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var stopWatch = Stopwatch.StartNew();

            stopWatch.Start();
            //Get Bank Exchange Rates
            BNCRManager bank          = new BNCRManager();
            var         exchangeRates = bank.GetExchangeRate();

            List <ShippingRate> rates = new List <ShippingRate>();

            FedExManager fedExManager = new FedExManager();

            rates.AddRange(fedExManager.GetRates());

            USPSManager uspsManager = new USPSManager();

            rates.AddRange(uspsManager.GetRates());

            var ratesInColones = rates.Select(c => { c.Price = c.Price * exchangeRates.PurchasePrice; return(c); }).ToList();

            stopWatch.Stop();
            Console.WriteLine($"Sequential Time {stopWatch.ElapsedMilliseconds}");

            Console.WriteLine("Shipping Method          Price (Colones)");
            foreach (var rate in rates)
            {
                Console.WriteLine($"{rate.Method}           {rate.Price}");
            }

            //Console.ReadKey();
        }
Ejemplo n.º 2
0
        private static long CalculateParallel()
        {
            var stopWatch = Stopwatch.StartNew();

            stopWatch.Start();
            ConcurrentBag <ShippingRate> rates = new ConcurrentBag <ShippingRate>();

            var providers = new List <IShippingManager>
            {
                //Get FedEx
                new FedExManager(),
                //Get USPS
                new USPSManager()
            };

            Task <DolarExchangeRate> exchangeRates = Task.Factory.StartNew(() =>
            {
                //Get Bank Exchange Rates
                IBankManager bank = new BNCRManager();
                return(bank.GetExchangeRate());
            });

            Parallel.ForEach(providers, provider =>
            {
                foreach (var rate in provider.GetRates())
                {
                    rates.Add(rate);
                }
            });

            Task.WaitAll(new Task[] { exchangeRates, /*fedexTask, uspsTask*/ });

            var ratesInColones = rates.Select(c => { c.Price = c.Price * exchangeRates.Result.PurchasePrice; return(c); }).ToList();

            stopWatch.Stop();
            return(stopWatch.ElapsedMilliseconds);
        }
Ejemplo n.º 3
0
        private static long CalculateSequential()
        {
            var stopWatch = Stopwatch.StartNew();

            stopWatch.Start();
            //Get Bank Exchange Rates
            BNCRManager bank          = new BNCRManager();
            var         exchangeRates = bank.GetExchangeRate();

            List <ShippingRate> rates = new List <ShippingRate>();

            FedExManager fedExManager = new FedExManager();

            rates.AddRange(fedExManager.GetRates());

            USPSManager uspsManager = new USPSManager();

            rates.AddRange(uspsManager.GetRates());

            var ratesInColones = rates.Select(c => { c.Price = c.Price * exchangeRates.PurchasePrice; return(c); }).ToList();

            stopWatch.Stop();
            return(stopWatch.ElapsedMilliseconds);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            try
            {
                var stopWatch = Stopwatch.StartNew();
                stopWatch.Start();
                //List<ShippingRate> rates = new List<ShippingRate>();
                ConcurrentBag <ShippingRate> rates = new ConcurrentBag <ShippingRate>();

                var providers = new List <IShippingManager>
                {
                    //Get FedEx
                    new FedExManager(),
                    //Get USPS
                    new USPSManager()
                };

                //IShippingManager fedEx = new FedExManager();
                //IShippingManager usps = new USPSManager();

                Task <DolarExchangeRate> exchangeRates = Task.Factory.StartNew(() =>
                {
                    //Get Bank Exchange Rates
                    IBankManager bank = new BNCRManager();
                    return(bank.GetExchangeRate());
                });

                //Task<List<ShippingRate>> fedexTask = Task.Factory.StartNew(() =>
                //{
                //    return fedEx.GetRates();
                //});

                //Task<List<ShippingRate>> uspsTask = Task.Factory.StartNew(() =>
                //{
                //    return usps.GetRates();
                //});

                Parallel.ForEach(providers, provider =>
                {
                    foreach (var rate in provider.GetRates())
                    {
                        rates.Add(rate);
                    }
                });

                Task.WaitAll(new Task[] { exchangeRates, /*fedexTask, uspsTask*/ });

                //rates.AddRange(fedexTask.Result);
                //rates.AddRange(uspsTask.Result);

                var ratesInColones = rates.Select(c => { c.Price = c.Price * exchangeRates.Result.PurchasePrice; return(c); }).ToList();

                stopWatch.Stop();
                Console.WriteLine($"Parallel Time {stopWatch.ElapsedMilliseconds}");

                Console.WriteLine("Shipping Method          Price (Colones)");
                foreach (var rate in ratesInColones)
                {
                    Console.WriteLine($"{rate.Method}           {rate.Price}");
                }
            }
            catch (AggregateException ae)
            {
                ae = ae.Flatten();
                foreach (var ex in ae.InnerExceptions)
                {
                    if (ex is OperationCanceledException)
                    {
                        continue;
                    }
                    else
                    {
                        //log the exception
                    }
                }
            }
            catch (Exception)
            {
                //log the exception
            }

            Console.ReadKey();
        }