user5724527
user5724527

Reputation:

How to make reading, processing and creating files faster?

I have some sample code.

        foreach (var currentFile in currentDirectory)
        {
            string[] contents = File.ReadAllText(currentFile).Split('%');
            using (XmlWriter writer = XmlWriter.Create(currentFile.Replace(".txt",".xml")))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("INFO");
                writer.WriteStartElement("INFO");
                writer.WriteElementString("USER", contents[1]);
                writer.WriteElementString("USERDATA", contents[2]);
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }

I have text file with text like that: MYNICKNAME % A LOT OF DATA. Now i am trying to make this programm multithreaded. What direction should i dig? async/await? i am really new to multithreading.

Upvotes: 0

Views: 121

Answers (1)

Chris
Chris

Reputation: 2019

You could change the foreach to Parallel.ForEach.

Sample: How to: Write a Simple Parallel.ForEach Loop

And a similar question: Read and process files in parallel C#

Upvotes: 1

Related Questions