c# - 接收网 : how to read all files simultaneously and on different threads

标签 c# system.reactive

目前,我能够使用响应式(Reactive) C# 使用以下代码一次一行地逐行读取文件夹中的文件。

现在我想在不同线程上同时读取所有文件以加快处理速度。如何使用响应式扩展 C# 实现此目的。

static void Main(string[] args)
    {
        foreach (string file in Directory.EnumerateFiles(@"C:\Test\", "*.txt"))
        {
            File.ReadLines(file)
                .ToObservable()
                .Subscribe(Console.WriteLine);
        }

        Console.ReadLine();
    }

最佳答案

你可以使用Parallel.ForEach;

var fileList = Directory.EnumerateFiles(@"C:\Test\", "*.txt").ToList();
Parallel.ForEach(fileList, file =>
{
    File.ReadLines(file)
        .ToObservable()
        .Subscribe(Console.WriteLine);
});

关于c# - 接收网 : how to read all files simultaneously and on different threads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48422980/

相关文章:

c# - string.GetHashCode() 对于 nlp 中的单词词典是否足够?

c# - Process.Start() 在 Windows 10/Chrome 上去掉井号和之后的所有内容

wpf - 将可见性绑定(bind)到 ReactiveCommand CanExecute

c# - 加入 Rx 流

visual-studio - f# 的响应式(Reactive)扩展

c# - C# 如何处理调用结构上的接口(interface)方法?

c# - Async WebClient 不是真正的异步?

c# - 未处理的异常终止执行时退出代码?

c# - 在订阅中调用 Task.Factory.StartNew(async () => {}) 通常是可疑的吗?

c# - 处理项目时缓冲