直接贴代码了:

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. English--美式发音

    English|美式发音 本文,总结了自己在学习美音的一些感悟,希望大家学习愉快!enjoy~ 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力求不含任何的自我感情色彩 ...

  2. maven 学习---转换基于Maven的Web应用程序支持Eclipse IDE

    在上一节教程中,使用Maven创建了一个Web应用程序.这里有一个指南,告诉你如何转换Web应用程序到Eclipse IDE支持的形式. 注意,通过WTP工具Eclipse IDE支持Web应用程序, ...

  3. time,datetime,random,os,sys,hashlib,logging,configparser,re模块

    #-----time模块----- print(help(time)) #打印time帮助文档 print(time.time()) #打印时间戳 1569824501.6265268 time.sl ...

  4. 使用类似GeoJson的数据生成物体(建筑等)的功能逻辑

    GeoJson作为一种模型传输格式, 用的最多的就是地图里面的各种简单模型了, 比如下图中很贴切的俄罗斯方块楼: 它的格式大概就是下面这样: { "type": "Fea ...

  5. idea中的插件,可以快速将类中的属性转换成Json字符串

    当我们想要测试接口的时候,难免会根据一个类,一个一个的写json数据,当属性比较少时还行,但当属性多的时候就比较麻烦了, 为了解决这个问题,我们可以安装第三方的插件来快速生成json字符串. 步骤如下 ...

  6. docker 中,修改了 mysql 配置,如何重启 mysql?

    直接重新启动容器: docker restart containerID/containerName

  7. nginx开启网站目录浏览功能

    一.开启全站目录浏览功能 编辑nginx.conf, 在http下面添加以下内容: autoindex on; # 开启目录文件列表 autoindex_exact_size on; # 显示出文件的 ...

  8. 树莓派autossh反向隧道

    本来我是将树莓派连接到路由器,从而在电脑端通过IP访问.远在局域网之外的队友怎么访问呢? ssh反向隧道 它的原理比较简单: 树莓派主动向某公网服务器建立ssh连接,并请求公网服务器开启一个额外的SS ...

  9. webapi基于单请求封装多请求的设计【转】

    怎么说,单请求封装多请求,这句话确实有点绕了,但还是要看清楚,想明白这到底是怎么一回事,单请求即一次请求(get,post,put,delete),封闭多请求,即在客户端发送的一个请求中可能包含多个子 ...

  10. 【未完成】【plsql】与oracle配置

    待写——————————————————————————————————————-