I’m trying to create a parallel foreach loop to processes a fasta file.
I’ve loaded the fasta file into a ConcurrentDictionary and have the parallel
foreach setup but when I go to process the ConcurrentDictionary my char array
throws an error about being blank even though the ConcurrentDictionary value
is never blank. I tried to debug but with the multiple threads it was very difficult
to tell what was going on.
Thank you
Casey
Parallel.ForEach(fastainfomation, info =>
{
line = info.Key;
name = info.Key;
secondline = info.Value;
if (name != "")
{
name = name.TrimEnd('\r', '\n');
char[] secondlineprimer = secondline.ToCharArray();
string primer = "";
string primername = "";
string primergroup = "";
for (int ii = 0; ii < 8; ii++)
{
primer += secondlineprimer[ii];
} ...
The fact that you’re not declaring
line,nameetc within the lambda expression suggests that they’re declared elsewhere – which means that they’ll be shared by all the invocations. That in itself is enough to make your code unsafe.You should also use
Substringrather than repeated string concatenation like this…Beyond that, your question doesn’t really contain enough information to help you further – please read writing the perfect question and pay more attention to clarity.