我正在尝试做这样的事情:
我有 3 个包含字符串的文件
- 读取 3 个文件并创建包含这 3 个文件内容的第四个文件
- 读取应该通过并行运行的线程完成
- 第 4 个线程写入第 4 个文件
我的问题是,你如何正确地阅读? ,如何确保第四个线程仅在所有文件都变为红色后才运行,如何将字符串内容获取到第四个线程?
读取文件后,第四个文件应包含按词典顺序排列的字符串,删除任何空格、符号和重复单词(无需给出其实现,只需建议在何处编码以及如何正确执行)
我使用了任务,我想知道如何使用线程来完成任务 在此代码中,字符串数组用于演示文件
如何在每个线程的“run”函数中正确读取文件?
using System;
using System.Threading.Tasks;
using System.IO;
using System.Text;
class Program {
static void Main() {
StringBuilder stringToRead = new StringBuilder();
StringReader reader;
Task [] tasks = new Task[3];
string [] filz = {"aaa" , "bbb" , "ccc"};
string [] red = new string[filz.Length];
foreach(string str in filz){
stringToRead.AppendLine(str);
}
for (int i = 0; i < tasks.Length ; i++)
{
tasks[i] = Task.Run(() => Console.WriteLine(i)); // it prints 3 , 3 , 3
}
try {
Task.WaitAll(tasks);
}
catch (AggregateException ae) {
Console.WriteLine("One or more exceptions occurred: ");
foreach (var ex in ae.Flatten().InnerExceptions)
Console.WriteLine(" {0}", ex.Message);
}
Console.WriteLine("Status of completed tasks:");
foreach (var t in tasks)
Console.WriteLine(" Task #{0}: {1}", t.Id, t.Status);
//now:
//4th thread that will writh the 3 previous files that have been red and writh it to a new file
}
}
最佳答案
以下是在 .NET 框架应用程序中使用 wait 的解决方案:
这是一个按钮点击事件,注意函数定义中的“async”:
private async void button1_Click(object sender, EventArgs e)
{
List<Task> tasks = new List<Task>();
string path = "C:\\Temp\\testfileread\\";
Task<string> file1Read = ReadTextAsync(Path.Combine(path, "test1.txt"));
Task<string> file2Read = ReadTextAsync(Path.Combine(path, "test2.txt"));
Task<string> file3Read = ReadTextAsync(Path.Combine(path, "test3.txt"));
await Task.WhenAll(file1Read, file2Read, file3Read);
string text1 = await file1Read;
string text2 = await file2Read;
string text3 = await file3Read;
await WriteTextAsync(Path.Combine(path, "result.txt"), text1 + text2 + text3);
}
以下是读写函数:
private async Task<string> ReadTextAsync(string filePath)
{
using (var reader = File.OpenText(filePath))
{
var fileText = await reader.ReadToEndAsync();
return fileText;
}
}
private async Task WriteTextAsync(string filePath, string value)
{
using (StreamWriter writer = File.CreateText(filePath))
{
await writer.WriteAsync(value);
}
}
代码几乎相同,但这里是在 .NET Core 控制台应用程序中完成的解决方案:
static void Main(string[] args)
{
try
{
var result = combineFiles().Result;
}
catch (ArgumentException aex)
{
Console.WriteLine($"Caught ArgumentException: {aex.Message}");
}
}
static async Task<bool> combineFiles()
{
List<Task> tasks = new List<Task>();
string path = "C:\\Temp\\testfileread\\";
Task<string> file1Read = ReadTextAsync(Path.Combine(path, "test1.txt"));
Task<string> file2Read = ReadTextAsync(Path.Combine(path, "test2.txt"));
Task<string> file3Read = ReadTextAsync(Path.Combine(path, "test3.txt"));
await Task.WhenAll(file1Read, file2Read, file3Read);
string text1 = await file1Read;
string text2 = await file2Read;
string text3 = await file3Read;
await WriteTextAsync(Path.Combine(path, "result.txt"), text1 + text2 + text3);
Console.WriteLine("Finished combining files");
Console.ReadLine();
return true;
}
private static async Task<string> ReadTextAsync(string filePath)
{
using (var reader = File.OpenText(filePath))
{
var fileText = await reader.ReadToEndAsync();
return fileText;
}
}
private static async Task WriteTextAsync(string filePath, string value)
{
using (StreamWriter writer = File.CreateText(filePath))
{
await writer.WriteAsync(value);
}
}
不要忘记所需的使用行:
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
关于C# 线程读写文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57824812/