Parallel.ForEach AgregateException - Sometimes I have problems using foreach in parallel

0

Sometimes I have problems using the foreach in parallel (I have a for within the foreach that adds values to a list), when the number of iterations per thread is large I get that exception. Sometimes it is slower than the foreach normal, I do not know how to evaluate the situation indicated to use parallel, if someone could guide me it would be great. This is what I have inside the function

Parallel.For(MemoryRegion, r => {

            if (pattern.Length > 1)
            {
                for (int i = 0; i < region.Length; i++)
                    if (region[i] == pattern[0])
                        for (int x = 1; x < pattern.Length; x++)
                            if (region.Length <= i + pattern.Length) break;
                            else if (pattern[x] != region[i + x])
                            {
                                if (region[i + x] == pattern[0])break;
                                else if (x == pattern.Length - 1)
                                { i += x; break; }
                                break;
                            }
                            else if (x == pattern.Length - 1)
                                addresses.Add(new IntPtr((int)r.BaseAddress + i));
            }
            else for (int i = 0; i < region.Length; i++)
                    if (region[i] == pattern[0])
                        addresses.Add(new IntPtr((int)r.BaseAddress + i));
    
asked by Mr.Noone 10.09.2016 в 02:53
source

0 answers