I am trying to do something like this:
I have 3 files which contain strings
- read 3 files and create a 4th file which contains the 3 files content
- the reading is suppose to be done with threads which will run in parallel
- 4th thread to write for the 4th file
my questions are , how do you do the reading properly ? , how do you make sure that the 4th thread will only run after all files have been red, how do you get the strings content to the 4th thread ?
after reading the files , the 4th files should contain the strings in lexicography order, delate any spaces , signs and repeated words (no need to give implementation of that, just recommend where to code this and how to do it properly)
I used tasks , I want to know how to use threads as well for that in this code the strings array is to demonstrate the files
how do i properly read the file in the "run" function of each thread?
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
}
}