zoukankan      html  css  js  c++  java
  • [原]C#多线程异步操作

    AsyncEnumerator版

                BlockingCollection<string> idsToProcess = new BlockingCollection<string>();
                idsToProcess.Add("a");
                idsToProcess.Add("b");
                idsToProcess.Add("c");
                Timer t = null;
                t = new Timer(async _ =>
                {
                    idsToProcess.CompleteAdding();
                    await t.DisposeAsync();
                }, null, 5000, Timeout.Infinite);
                idsToProcess.GetConsumingEnumerable().ParallelForEachAsync(async id =>
                {
                    await Task.Run(() =>
                    {
                        Console.WriteLine(id);
                    });
                }).GetAwaiter().GetResult();

    Nito.AsyncEx版

                BlockingCollection<string> idsToProcess = new BlockingCollection<string>();
                idsToProcess.Add("a");
                idsToProcess.Add("b");
                idsToProcess.Add("c");
                Timer t = null;
                t = new Timer(async _ =>
                {
                    idsToProcess.CompleteAdding();
                    await t.DisposeAsync();
                }, null, 5000, Timeout.Infinite);
                Parallel.ForEach(idsToProcess.GetConsumingEnumerable(),
                    id =>
                    {
                        AsyncContext.Run(async () =>
                        {
                            await Task.Run(() =>
                            {
                                Console.WriteLine(id);
                            }); 
                        });
                    });
  • 相关阅读:
    Storm 第一章 核心组件及编程模型
    四 Hive整合HBase
    Hbase第五章 MapReduce操作HBase
    swift 附属脚本
    swift 方法
    swift 属性
    swift 类和结构体
    swift 枚举类型
    swift 闭包
    Swift 函数
  • 原文地址:https://www.cnblogs.com/yzpopulation/p/12398841.html
Copyright © 2011-2022 走看看