Esempio n. 1
0
        static public void ProcessingByTPL_StraightForwardImplementation()
        {
            const string pathToFiles = @"..\..\..\..\DataFiles";
            string[] files = Directory.GetFiles(pathToFiles, "*.txt", SearchOption.AllDirectories);

            var loadDataFromFileBlock = new TransformBlock<string[], List<CustomerTextData>>(fileItems =>
            {
                var factory = new CustomerTextDataFactory();
                return new List<CustomerTextData>(Array.ConvertAll(fileItems, factory.LoadFromFile));
            });
            var filterBlock = new TransformBlock<List<CustomerTextData>, List<CustomerTextData>>(textDataList =>
            {
                var filter = new FilterTextData(5);
                return textDataList.Where(filter.Run).ToList();
            });
            var toListBlock = new TransformManyBlock<List<CustomerTextData>, CustomerTextData>(textDataList =>
            {
                var queue = new ConcurrentQueue<CustomerTextData>();
                textDataList.ForEach(queue.Enqueue);
                return queue;
            });
            var action = new ActionBlock<CustomerTextData>(textData =>
            {
                var weight = new WeightTextData();
                int result = weight.Run(textData);
                Trace.WriteLine(result);
                Console.WriteLine(result);
            });

            loadDataFromFileBlock.LinkTo(filterBlock);
            filterBlock.LinkTo(toListBlock);
            toListBlock.LinkTo(action);

            loadDataFromFileBlock.Completion.ContinueWith(t =>
            {
                if (t.IsFaulted) ((IDataflowBlock)filterBlock).Fault(t.Exception);
                else filterBlock.Complete();
            });
            filterBlock.Completion.ContinueWith(t =>
            {
                if (t.IsFaulted) ((IDataflowBlock)toListBlock).Fault(t.Exception);
                else toListBlock.Complete();
            });
            toListBlock.Completion.ContinueWith(t =>
            {
                if (t.IsFaulted) ((IDataflowBlock)action).Fault(t.Exception);
                else action.Complete();
            });

            loadDataFromFileBlock.Post(files);
            loadDataFromFileBlock.Complete();
            action.Completion.Wait();
        }
Esempio n. 2
0
        public ActionBlock <CustomerTextData> Process()
        {
            var action = new ActionBlock <CustomerTextData>(textData =>
            {
                var weight = new WeightTextData();
                int result = weight.Run(textData);
                Trace.WriteLine(result);
                Console.WriteLine(result);
            });

            flow.Add(action);
            return(action);
        }
Esempio n. 3
0
        public TransformBlock <BuilderCustomerData, ItemReport> Processing()
        {
            var block = new TransformBlock <BuilderCustomerData, ItemReport>(data =>
            {
                var weight = new WeightTextData();
                int result = weight.Run(data.CustomerData);
                return(new ItemReport {
                    Enabled = true, FilePath = data.PathToFile.File, Weight = result
                });
            });

            return(block);
        }
Esempio n. 4
0
        public FlowBuilderFactory_v3 Processing()
        {
            var block = new TransformBlock <object, object>(dataObject =>
            {
                var data = (BuilderCustomerData)dataObject;
                if (data.Enabled)
                {
                    var weight       = new WeightTextData();
                    int weightResult = weight.Run(data.CustomerData);
                    return(new ItemReport {
                        Enabled = true, FilePath = data.PathToFile.File, Weight = weightResult
                    });
                }
                return(new ItemReport {
                    Enabled = false, FilePath = data.PathToFile.File
                });
            });

            Flow.Add(block);
            return(this);
        }
Esempio n. 5
0
        static public void ProcessingByTPL_StraightForwardImplementation()
        {
            const string pathToFiles = @"..\..\..\..\DataFiles";

            string[] files = Directory.GetFiles(pathToFiles, "*.txt", SearchOption.AllDirectories);

            var loadDataFromFileBlock = new TransformBlock <string[], List <CustomerTextData> >(fileItems =>
            {
                var factory = new CustomerTextDataFactory();
                return(new List <CustomerTextData>(Array.ConvertAll(fileItems, factory.LoadFromFile)));
            });
            var filterBlock = new TransformBlock <List <CustomerTextData>, List <CustomerTextData> >(textDataList =>
            {
                var filter = new FilterTextData(5);
                return(textDataList.Where(filter.Run).ToList());
            });
            var toListBlock = new TransformManyBlock <List <CustomerTextData>, CustomerTextData>(textDataList =>
            {
                var queue = new ConcurrentQueue <CustomerTextData>();
                textDataList.ForEach(queue.Enqueue);
                return(queue);
            });
            var action = new ActionBlock <CustomerTextData>(textData =>
            {
                var weight = new WeightTextData();
                int result = weight.Run(textData);
                Trace.WriteLine(result);
                Console.WriteLine(result);
            });

            loadDataFromFileBlock.LinkTo(filterBlock);
            filterBlock.LinkTo(toListBlock);
            toListBlock.LinkTo(action);

            loadDataFromFileBlock.Completion.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    ((IDataflowBlock)filterBlock).Fault(t.Exception);
                }
                else
                {
                    filterBlock.Complete();
                }
            });
            filterBlock.Completion.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    ((IDataflowBlock)toListBlock).Fault(t.Exception);
                }
                else
                {
                    toListBlock.Complete();
                }
            });
            toListBlock.Completion.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    ((IDataflowBlock)action).Fault(t.Exception);
                }
                else
                {
                    action.Complete();
                }
            });

            loadDataFromFileBlock.Post(files);
            loadDataFromFileBlock.Complete();
            action.Completion.Wait();
        }