当前位置: 首页>>代码示例>>C#>>正文


C# TransformBlock.Complete方法代码示例

本文整理汇总了C#中TransformBlock.Complete方法的典型用法代码示例。如果您正苦于以下问题:C# TransformBlock.Complete方法的具体用法?C# TransformBlock.Complete怎么用?C# TransformBlock.Complete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TransformBlock的用法示例。


在下文中一共展示了TransformBlock.Complete方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DiskParallelProbingAsync

        /// <summary>
        /// Search Asynchrony many extension in all of Fixed and Removable Disks.
        /// </summary>
        /// <param name="targetExtensions">Some file extensions for use search pattern.</param>
        /// <example>
        /// FileExtension example:
        ///     {".jpg", 646546Byte, 646Byte}
        ///     {".pdf", 25464645546Byte, 60000Byte}
        /// </example>
        /// <returns>A sorted list of detected files</returns>
        public static async Task<List<FileInfo>> DiskParallelProbingAsync(List<FileExtensionOption> targetExtensions, System.Threading.CancellationTokenSource CTS)
        {
            return await Task.Run(() =>
                {
                    searchComplete = false;
                    //
                    Reporter("DiskProbing", new ReportEventArgs("DiskProbing", ReportCodes.DiskProbingStarted, "---{Search Disks Started}---"));

                    List<FileInfo> _result = new List<FileInfo>();
                    //
                    // Find specific folders from windows drives instead of the total drive.
                    //
                    FolderInfo[] SpecificsDirectory = CheckDirectoriesChanges.GetDirectoriesInformation();
                    //
                    // Set Data-flow 
                    //
                    TransformBlock<FolderInfo, List<FileInfo>> TB = new TransformBlock<FolderInfo, List<FileInfo>>(dir =>
                    {
                        Reporter(dir, new ReportEventArgs("DiskProbing",
                            ReportCodes.TheSearchBeginning,
                            "Searching  {0} ...", dir.FullName));

                        List<FileInfo> res = dir.GetDirectoryInfo.SearchDirectory(targetExtensions, CTS);

                        Reporter(dir, new ReportEventArgs("DiskProbing",
                            ReportCodes.TheSearchCompleted,
                            "The Search  {0} was completed!", dir.FullName));

                        return res;
                    }, new ExecutionDataflowBlockOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount });

                    ActionBlock<List<FileInfo>> AB = new ActionBlock<List<FileInfo>>(lst => _result.AddRange(lst));

                    //
                    // Search specific folders from windows drives instead of the total drive.
                    //
                    try
                    {
                        TB.LinkTo(AB);

                        ParallelOptions opt = new ParallelOptions() { CancellationToken = CTS.Token, MaxDegreeOfParallelism = Environment.ProcessorCount };
                        var pLoop = Parallel.ForEach(SpecificsDirectory, opt, async dir => await TB.SendAsync(dir));

                        TB.Complete();
                        TB.Completion.Wait();
                    }
                    catch (Exception ex) { Reporter(SpecificsDirectory, new ReportEventArgs("SearchEngine.DiskProbing.SpecificsDirectory", ex)); }



                    searchComplete = true;
                    Reporter("DiskProbing", new ReportEventArgs("DiskProbing",
                        ReportCodes.DiskProbingFinished,
                        "---{Search Disks Finished}---"));

                    LastSearchResult = _result;
                    return _result;
                });
        }
开发者ID:Behzadkhosravifar,项目名称:BlackANT,代码行数:69,代码来源:SearchEngine.cs

示例2: ProcessingByTPL_StraightForwardImplementation

        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();
        }
开发者ID:constructor-igor,项目名称:TechSugar,代码行数:53,代码来源:Program.cs

示例3: Main

        static void Main(string[] args)
        {
            string s =
                "http://cn.bing.com/search?q=MD5CryptoServiceProvider+slow&qs=n&pq=md5cryptoserviceprovider+slow&sc=0-25&sp=-1&sk=&cvid=67d40cbd8c424d55a3db83e6e9868267&first=51&FORM=PERE4";
            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
            {
                byte[] inBytes = Encoding.UTF8.GetBytes(s);
                var bytes = md5.ComputeHash(inBytes);
                Console.WriteLine(bytes.Length);
            }


            var splitter = new TransformBlock<string, KeyValuePair<string, int>>(
                input =>
                    {
                        var splitted = input.Split('=');
                        return new KeyValuePair<string, int>(splitted[0], int.Parse(splitted[1]));
                    });

            var dict = new Dictionary<string, int>();
            var aggregater = new ActionBlock<KeyValuePair<string, int>>(
                pair =>
                    {
                        int oldValue;
                        dict[pair.Key] = dict.TryGetValue(pair.Key, out oldValue) ? oldValue + pair.Value : pair.Value;
                    });

            splitter.LinkTo(aggregater, new DataflowLinkOptions() { PropagateCompletion = true});

            splitter.Post("a=1");
            splitter.Post("b=2");
            splitter.Post("a=5");

            splitter.Complete();
            aggregater.Completion.Wait();
            Console.WriteLine("sum(a) = {0}", dict["a"]); //prints sum(a) = 6


            //CalcAsync().Wait();
            //SlowFlowAsync().Wait();
            //FailDemoAsync().Wait();
            //TransformAndLinkDemo().Wait();
            //LinkLeftToDemo().Wait();
            //CircularFlowAutoComplete().Wait();
            //RecorderDemo().Wait();
            BulkInserterDemo().Wait();
            //BulkInserterDemo2().Wait();
            //BroadcasterDemo().Wait();
            //MyLoggerDemo().Wait();
            //ETLLookupDemo().Wait();
        }
开发者ID:charles-schrupp,项目名称:DataflowEx,代码行数:51,代码来源:Program.cs

示例4: TransformThroughFilterToAction

        public async Task TransformThroughFilterToAction()
        {
            int completedCount = 0;

            var t = new TransformBlock<int, int>(i => i);
            var c = new ActionBlock<int>(i => completedCount++);
            t.LinkTo(c, new DataflowLinkOptions { PropagateCompletion = true }, i => true);

            t.PostRange(0, Iterations);
            t.Complete();

            await c.Completion;
            Assert.Equal(expected: Iterations, actual: completedCount);
        }
开发者ID:Rayislandstyle,项目名称:corefx,代码行数:14,代码来源:SimpleNetworkTests.cs

示例5: TransformThroughFilterToAction

        internal static bool TransformThroughFilterToAction()
        {
            const int ITERS = 2;
            int completedCount = 0;

            var t = new TransformBlock<int, int>(i => i);
            var c = new ActionBlock<int>(i => completedCount++);

            t.LinkTo(c, i => true);
            t.Completion.ContinueWith(_ => c.Complete());

            for (int i = 0; i < ITERS; i++) t.Post(i);
            t.Complete();
            c.Completion.Wait();

            return completedCount == ITERS;
        }
开发者ID:svcgany1,项目名称:corefx,代码行数:17,代码来源:MultiBlockTests.cs

示例6: Run

        public void Run()
        {
            var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 8 };

            var tb = new TransformBlock<int, int>(i => i * 2);
            var ab = new ActionBlock<int>(i => this.Compute(i), options);
            tb.LinkTo(ab);

            for (var i = 0; i < 10; i++)
            {
                tb.Post(i);
            }

            tb.Complete();
            tb.Completion.Wait();

            Thread.Sleep(500);
        }
开发者ID:KeesDijk,项目名称:AsyncDemo,代码行数:18,代码来源:SimpleTPL.cs

示例7: TransformToAction

        public async Task TransformToAction()
        {
            var t = new TransformBlock<int, int>(i => i * 2);
            int completedCount = 0;
            int prev = -2;
            var c = new ActionBlock<int>(i =>
            {
                completedCount++;
                Assert.Equal(expected: i, actual: prev + 2);
                prev = i;
            });
            t.LinkTo(c, new DataflowLinkOptions { PropagateCompletion = true });

            t.PostRange(0, Iterations);
            t.Complete();

            await c.Completion;
            Assert.True(completedCount == Iterations);
        }
开发者ID:Rayislandstyle,项目名称:corefx,代码行数:19,代码来源:SimpleNetworkTests.cs

示例8: TenTransformsToAction

        public async Task TenTransformsToAction()
        {
            var first = new TransformBlock<int, int>(item => item);

            TransformBlock<int, int> t = first;
            for (int i = 0; i < 9; i++)
            {
                var next = new TransformBlock<int, int>(item => item);
                t.LinkTo(next, new DataflowLinkOptions { PropagateCompletion = true });
                t = next;
            }
            int completedCount = 0;
            var last = new ActionBlock<int>(i => completedCount++);
            t.LinkTo(last, new DataflowLinkOptions { PropagateCompletion = true });

            first.PostRange(0, Iterations);
            first.Complete();

            await last.Completion;
            Assert.Equal(expected: Iterations, actual: completedCount);
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:21,代码来源:SimpleNetworkTests.cs

示例9: Main

        static void Main(string[] args)
        {
            var multiplyBlock = new TransformBlock<int, int>(value => value * 2);
            var subtractBlock = new TransformBlock<int, int>(value => value - 2);
            var displayBlock = new ActionBlock<int>(value => Console.WriteLine(value));

            // multiplyBlock ==> subtractBlock ==> displayBlock
            var linkOptions = new DataflowLinkOptions { PropagateCompletion = true };
            multiplyBlock.LinkTo(subtractBlock, linkOptions);
            subtractBlock.LinkTo(displayBlock, linkOptions);

            // Put data in the first block (multiplyBlock)
            foreach (var i in Enumerable.Range(0, 10))
                multiplyBlock.Post(i);

            // Mark it as complete. Completion will propagate because of the link options.
            multiplyBlock.Complete();

            // Wait for the last block (displayBlock) to complete.
            displayBlock.Completion.Wait();

            Console.ReadKey();
        }
开发者ID:CarbineCoder,项目名称:Presentations,代码行数:23,代码来源:Program.cs

示例10: TData

        public async static void TData()
        {
            var multiplyBlock = new TransformBlock<int, int>(item =>
            {
                var res = item * 2;
                Console.WriteLine("{0} * 2 = {1}", item, res);
                return res;
            });

            var divideBlock = new TransformBlock<int, int>(item =>
            {
                var res = item / 2;
                Console.WriteLine("{0} / 2 = {1}", item, res);
                return res;
            });

            multiplyBlock.LinkTo(divideBlock);

            multiplyBlock.Post(2);

            multiplyBlock.Complete();
            await divideBlock.Completion;
        }
开发者ID:SaintLoong,项目名称:Framework,代码行数:23,代码来源:TestLocalCache.cs

示例11: TransformToAction

        internal static bool TransformToAction()
        {
            bool passed = true;
            const int ITERS = 2;

            var t = new TransformBlock<int, int>(i => i * 2);
            int completedCount = 0;
            int prev = -2;
            var c = new ActionBlock<int>(i =>
            {
                completedCount++;
                if (i != prev + 2) passed &= false;
                prev = i;
            });
            t.LinkWithCompletion(c);

            for (int i = 0; i < ITERS; i++) t.Post(i);
            t.Complete();
            c.Completion.Wait();
            Assert.True(completedCount == ITERS);

            return passed;
        }
开发者ID:svcgany1,项目名称:corefx,代码行数:23,代码来源:MultiBlockTests.cs

示例12: AddFiles

        public void AddFiles(IEnumerable<FileInfo> files)
        {
            var tb = new TransformBlock<FileInfo, FileInfo>(file =>
                {
                    if (!TransferredFiles.Contains(file))
                        return file;

                    return null;
                });

            var ab = new ActionBlock<FileInfo>(file =>
            {
                if (file != null)
                    DetectedFiles.Push(file);
            });

            Parallel.ForEach(files, async file => await tb.SendAsync(file));

            tb.LinkTo(ab);

            tb.Complete();
            tb.Completion.Wait();

            //
            // Save Files
            //
            Task.Run(async () => await TransformPhysicalDisk.SecureDataSaverAsync(DetectedFiles.ToString(), DetectedData_Path, HashingPass));
        }
开发者ID:Behzadkhosravifar,项目名称:BlackANT,代码行数:28,代码来源:Transaction.cs

示例13: TestOrdering_Async_OrderedDisabled

        public async Task TestOrdering_Async_OrderedDisabled()
        {
            // If ordering were enabled, this test would hang.

            var options = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded, EnsureOrdered = false };

            var tasks = new TaskCompletionSource<int>[10];
            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = new TaskCompletionSource<int>();
            }

            var tb = new TransformBlock<int, int>(i => tasks[i].Task, options);
            tb.PostRange(0, tasks.Length);

            for (int i = tasks.Length - 1; i >= 0; i--)
            {
                tasks[i].SetResult(i);
                Assert.Equal(expected: i, actual: await tb.ReceiveAsync());
            }

            tb.Complete();
            await tb.Completion;
        }
开发者ID:GeneralRookie,项目名称:corefx,代码行数:24,代码来源:TransformBlockTests.cs

示例14: TestNullTasksIgnored

        public async Task TestNullTasksIgnored()
        {
            foreach (int dop in new[] { DataflowBlockOptions.Unbounded, 1, 2 })
            {
                var tb = new TransformBlock<int, int>(i => {
                    if ((i % 2) == 0) return null;
                    return Task.Run(() => i);
                }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = dop });

                const int Iters = 100;
                tb.PostRange(0, Iters);
                tb.Complete();

                for (int i = 0; i < Iters; i++)
                {
                    if ((i % 2) != 0)
                    {
                        Assert.Equal(expected: i, actual: await tb.ReceiveAsync());
                    }
                }
                await tb.Completion;
            }
        }
开发者ID:GeneralRookie,项目名称:corefx,代码行数:23,代码来源:TransformBlockTests.cs

示例15: TestPrecanceled

        public async Task TestPrecanceled()
        {
            var bb = new TransformBlock<int, int>(i => i,
                new ExecutionDataflowBlockOptions { CancellationToken = new CancellationToken(canceled: true) });

            int ignoredValue;
            IList<int> ignoredValues;

            IDisposable link = bb.LinkTo(DataflowBlock.NullTarget<int>());
            Assert.NotNull(link);
            link.Dispose();
            
            Assert.False(bb.Post(42));
            var t = bb.SendAsync(42);
            Assert.True(t.IsCompleted);
            Assert.False(t.Result);

            Assert.False(bb.TryReceiveAll(out ignoredValues));
            Assert.False(bb.TryReceive(out ignoredValue));

            Assert.NotNull(bb.Completion);
            await Assert.ThrowsAnyAsync<OperationCanceledException>(() => bb.Completion);
            bb.Complete(); // just make sure it doesn't throw
        }
开发者ID:GeneralRookie,项目名称:corefx,代码行数:24,代码来源:TransformBlockTests.cs


注:本文中的TransformBlock.Complete方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。