一个简单 System.Threading.Tasks.Dataflow.TransformBlock 示例
直接贴代码了:
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 示例的更多相关文章
- 一个简单 System.Threading.Tasks.Dataflow.BufferBlock 示例
直接贴代码了: using System; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; namespace ...
- 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”, 编写代码,生成一个 ...
- System.Threading.Tasks.Task引起的IIS应用程序池崩溃
问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应. 2. 从服务器端看(Windows Server 2008 + IIS 7.0 ...
- 一、并行编程 - 数据并行 System.Threading.Tasks.Parallel 类
一.并行概念 1.并行编程 在.NET 4中的并行编程是依赖Task Parallel Library(后面简称为TPL) 实现的.在TPL中,最基本的执行单元是task(中文可以理解为"任 ...
- System.Threading.Tasks.Task 任务引起的IIS应用程序池崩溃
转载:http://www.cnblogs.com/aaa6818162/p/4421305.html 问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状 ...
- System.Threading.Tasks
前言: 我们之前介绍了两种构建多线程软件的编程技术(使用异步委托或通过System.Threading的成员).这两个可以在任何版本的.NET平台工作. 关于System.Threading 的介绍 ...
- .Net多线程编程—System.Threading.Tasks.Parallel
System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel. ...
- 转载 Net多线程编程—System.Threading.Tasks.Parallel
.Net多线程编程—System.Threading.Tasks.Parallel System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Paral ...
- .NET 4.0 System.Threading.Tasks学习笔记
由于工作上的需要,学习使用了System.Threading.Tasks的使用,特此笔记下来. System.Threading.Tasks的作用: Tasks命名空间下的类试图使用任务的概念来解决线 ...
随机推荐
- Web Api 模型绑定 二
[https://docs.microsoft.com/zh-cn/aspnet/core/web-api/?view=aspnetcore-2.2] 1.ApiController属性使模型验证错误 ...
- Android不显示开机向导和开机气泡
修改好的代码下载地址: https://github.com/Vico-H/Launcher 不显示开机向导 修改Launcher2.java的代码 (文件位置: /alps/packages/app ...
- Spark(4)
Spark Core官网学习回顾 Speed disk 10x memory 100x Easy code interactive shell Unified Stack Batch Streamin ...
- 【Linux】扩展阿里云数据盘分区和文件系统
扩容云盘只是扩大存储容量,不会扩容文件系统 一.准备工作 在扩展数据盘扩展分区和文件系统前,请提前完成以下工作. 创建快照以备份数据,防止操作失误导致数据丢失. 通过ECS控制台或者API扩容云盘容量 ...
- 常用.gitignore
android开发 关键词:java,android,androidstudio 地址:https://www.gitignore.io/api/java,android,androidstudio ...
- 阿里云Web应用防火墙采用规则引擎、语义分析和深度学习引擎相结合的方式防护Web攻击
深度学习引擎最佳实践 {#concept_1113021 .concept} 阿里云Web应用防火墙采用多种Web攻击检测引擎组合的方式为您的网站提供全面防护.Web应用防火墙采用规则引擎.语义分析和 ...
- React中ref的三种用法 可以用来获取表单中的值 这一种类似document.getXXId的方式
import React, { Component } from "react" export default class MyInput extends Component { ...
- python中线程和进程的简单了解
python中线程和进程的简单了解 一.操作系统.应用程序 1.硬件:硬盘.cpu.主板.显卡........ 2.装系统(本身也是一个软件): 系统就是一个由程序员写出来的软件,该软件用于控制计 ...
- python requests访问https的链接,不打开fiddler的情况下不报错;若是打开fiddler则报ssl错误,请求中添加verify=False,会报警告;若不想看到警告,有3种方式;
import requests# import warnings# warnings.filterwarnings("ignore") #方法一#requests.packages ...
- WIN10安装64位JDK10.0.2详细完整教程(图文)
1.下载JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 1)如下图,点击红色箭头处: 2)如下图 ...