直接贴代码了:

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks.Dataflow; namespace DataFlowSample
{
class Program
{
static void Main()
{
var target = SetupPipeline();
target.Post(@"D:\Source\BenchmarkAndSpanExample-master\BenchmarkAndSpanExample");
Console.ReadLine();
} public static IEnumerable<string> GetFileNames(string path)
{
foreach (var fileName in Directory.EnumerateFiles(path, "*.cs"))
{
yield return fileName;
}
} public static IEnumerable<string> LoadLines(IEnumerable<string> fileNames)
{
foreach (var fileName in fileNames)
{
using (FileStream stream = File.OpenRead(fileName))
{
var reader = new StreamReader(stream);
string line = null;
while ((line = reader.ReadLine()) != null)
{
//WriteLine($"LoadLines {line}");
yield return line;
}
}
}
} public static IEnumerable<string> GetWords(IEnumerable<string> lines)
{
foreach (var line in lines)
{
string[] words = line.Split(' ', ';', '(', ')', '{', '}', '.', ',');
foreach (var word in words)
{
if (!string.IsNullOrEmpty(word))
yield return word;
}
}
} public static ITargetBlock<string> SetupPipeline()
{
var fileNamesForPath = new TransformBlock<string, IEnumerable<string>>(
path => GetFileNames(path)); var lines = new TransformBlock<IEnumerable<string>, IEnumerable<string>>(
fileNames => LoadLines(fileNames)); var words = new TransformBlock<IEnumerable<string>, IEnumerable<string>>(
lines2 => GetWords(lines2)); var display = new ActionBlock<IEnumerable<string>>(
coll =>
{
foreach (var s in coll)
{
Console.WriteLine(s);
}
}); fileNamesForPath.LinkTo(lines);
lines.LinkTo(words);
words.LinkTo(display);
return fileNamesForPath;
}
}
}

谢谢浏览!

一个简单 System.Threading.Tasks.Dataflow.TransformBlock 示例的更多相关文章

  1. 一个简单 System.Threading.Tasks.Dataflow.BufferBlock 示例

    直接贴代码了: using System; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; namespace ...

  2. vs2013c#测试using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1_CXY { class Program { stati

    首先安装Unit Test Generator.方法为:工具->扩展和更新->联机->搜索“图标为装有蓝色液体的小试管.Unit Test Generator”, 编写代码,生成一个 ...

  3. System.Threading.Tasks.Task引起的IIS应用程序池崩溃

    问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应. 2. 从服务器端看(Windows Server 2008 + IIS 7.0 ...

  4. 一、并行编程 - 数据并行 System.Threading.Tasks.Parallel 类

    一.并行概念 1.并行编程 在.NET 4中的并行编程是依赖Task Parallel Library(后面简称为TPL) 实现的.在TPL中,最基本的执行单元是task(中文可以理解为"任 ...

  5. System.Threading.Tasks.Task 任务引起的IIS应用程序池崩溃

    转载:http://www.cnblogs.com/aaa6818162/p/4421305.html 问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状 ...

  6. System.Threading.Tasks

    前言: 我们之前介绍了两种构建多线程软件的编程技术(使用异步委托或通过System.Threading的成员).这两个可以在任何版本的.NET平台工作. 关于System.Threading 的介绍 ...

  7. .Net多线程编程—System.Threading.Tasks.Parallel

    System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel. ...

  8. 转载 Net多线程编程—System.Threading.Tasks.Parallel

    .Net多线程编程—System.Threading.Tasks.Parallel   System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Paral ...

  9. .NET 4.0 System.Threading.Tasks学习笔记

    由于工作上的需要,学习使用了System.Threading.Tasks的使用,特此笔记下来. System.Threading.Tasks的作用: Tasks命名空间下的类试图使用任务的概念来解决线 ...

随机推荐

  1. SocketServer模块与简单并发服务器

    思维导图文件:https://files-cdn.cnblogs.com/files/benjieming/SocketServer%E6%A8%A1%E5%9D%97%E4%B8%8E%E7%AE% ...

  2. 小鸟初学Shell编程(一)认识Shell

    开篇介绍 Linux里非常的有用的一个功能,这个功能就叫Shell脚本. Shell脚本在我日常开发工作里也占了非常重要的角色,项目中一些简单的工作我们都可以使用Shell脚本来完成,比如定时删除日志 ...

  3. 【React】Redux入门 & store体验

    组件间传值联动是令人头疼的问题,尤其是一个组件影响多个其他组件状态变化的时候,常常需要一级一级与父组件传值,与父组件的兄弟组件传值等等, 如何化繁为简地处理‘牵一发动全身’的清理就是将所有组件的sta ...

  4. Qt 连接MySQL

    工程文件 QT += sql 举例 QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName(&q ...

  5. Apache:系统找不到指定的文件: No installed ConfigArgs for the service "Apache2"

    解决方法: 将以下内容保存成FixApacheError.reg文件(其中红色粗体Apache2改成报错的系统服务名称,如"RTX_HTTPServer"),导入系统注册表 Fix ...

  6. NACOS升级操作

    Server端 0.8.0及以上版本: 解压安装包后替换{nacos.home}/target/nacos-server.jar 删除{nacos.home}/plugins/cmdb/及{nacos ...

  7. python实验脚本

    day1 1.  任性猜数字 需求 : 猜数字, 猜3次.如果对了 就直接退出. 如果3次都错了 ,就询问 要不要继续. 实现点: 使用 whie 循环. count 进行计数.如果count > ...

  8. 代码审计-md5()函数

    <?php error_reporting(0); $flag = 'flag{test}'; if (isset($_GET['username']) and isset($_GET['pas ...

  9. day22_7.26面向对象之封装(接口与抽象)

    一.封装. 封装就是将丑陋复杂的隐式的细节隐藏到内部,对外提供简单的使用接口. 对外隐藏内部实现细节,并提供访问的接口.对内使用self操作. 二.为什么要封装? 对于一个计算机来说,不可能不使用机箱 ...

  10. SQL中group by的注意事项

    最最最最重要的: group by有一个原则,就是select后面所有的列中,没有使用聚合函数的列,必须出现在group by子句中. group by子句中的注意事项: 1,不能使用别名(因为执行顺 ...