Reputation: 301
First off, I apologize for the title, I am unsure of how to word this question.
I currently have a Parallel.ForEach
looping through a directory filled with .txt files.
What I am doing inside of this is grabbing specific data that varies from file-to-file.
Whenever all of the needed data is grabbed, it is then outputted to a file.
Everything is accurate except for the ID numbers inside each text file. The ID numbers are not lining up with their corresponding file. It's outputted in an arbitrary manner. The names and date's line up fine, but not the actual ID numbers inside of the text files. I am unsure of how to proceed with this as the code looks fine to me. The only thing I can think of is AppendAllText
is consistently opening and closing the final text file, which in turn disallows accurate data to be outputted. Below is my code.
Parallel.ForEach(directoryInfo.GetFiles("*.txt"), (file, state) =>
{
using StreamReader sr = File.OpenText(file.FullName);
string user = sr.ReadToEnd();
if (user.Contains("ID:"))
{
ID = IDRegex.Match(user).Value.Replace("ID:", string.Empty);
}
else if (user.Contains("ID="))
{
ID = IDDRegex.Match(user).Value.Replace("ID=", string.Empty);
}
this.Dispatcher.Invoke(() =>
{
//count++;
//Current.Content = file.Name;
if (user.Contains(users.Text))
{
File.AppendAllText(Idir + "IReport-" + IUser.Text + ".txt",
String.Format("{0,-16} {1,27}", file.Name.Replace(".txt", ""), (file.LastWriteTime.Date).ToString()).Replace("12:00:00 AM", "") +
String.Format("{0, 18}", ID) + Environment.NewLine + Environment.NewLine);
}
});
});
Upvotes: 0
Views: 1154
Reputation: 7140
Your problem is that because you've used Parallel.ForEach()
, the code you've posted is being run multiple times simutaneously by different threads. This is great for doing the work faster, but it can catch you out a few ways.
Your variable ID
is not declared in the code you've posted, which means it must be coming from somewhere else. This means that all the threads created by your use of Parallel.ForEach()
are sharing the same variable, overwriting each other's values, which is what you're seeing: the ID being written to the file isn't the one for this file, it's one from whichever thread happens to have touched that value most recently.
Declare a fresh variable inside your call to Parallel.ForEach()
to use as the ID, and all threads will have their own value that the others can't mess with. That should fix your problem.
Upvotes: 2