0

basically i'm trying run four functions together using task. these four functions runs their own foreach loop. when i'm executing these functions keep getting error. "Collection was modified; enumeration operation may not execute. "

so far methods I have tried:

method 1: fail

var tasks = new[] 
{ 
    Task.Factory.StartNew(() => function1(publishrequest,nextshiftRq, publishList)),
    Task.Factory.StartNew(() => function2(publishrequest,nextshiftRq, publishList)),
    Task.Factory.StartNew(() => function3(publishrequest,nextshiftRq, publishList)),
    Task.Factory.StartNew(() => function4(publishrequest,nextshiftRq, publishList))
};
Task.WaitAll(tasks);

method 2: fail

List<Task> tasks = new List<Task>();
tasks.Add(Task.Factory.StartNew(() => function1(publishrequest, nextshiftRq, publishList)));
tasks.Add(Task.Factory.StartNew(() => function2(publishrequest, nextshiftRq, publishList)));
tasks.Add(Task.Factory.StartNew(() => function3(publishrequest, nextshiftRq, publishList)));
tasks.Add(Task.Factory.StartNew(() => function4(publishrequest, nextshiftRq, publishList)));
Task.WaitAll(tasks.ToArray());

method 3: fail

Parallel.Invoke(
    () = {function1(publishrequest, nextshiftRq, publishList)},
    () = {function2(publishrequest, nextshiftRq, publishList)},
    () = {function3(publishrequest, nextshiftRq, publishList)},
    () = {function4(publishrequest, nextshiftRq, publishList)});

inside in functions

public void function1(publish p1,nextshift nx, List<publish> pub)
{
    foreach(publish in pub.ToList())
    {
       Evaluate.EvaluateShift(p1.publishShift.ShiftRpt,publish.SectionId);
       Evaluate.EvaluateShift(nx,publish.SectionId);
    }
}

//this function1 again call another function where another foreach loop is running.

//Evaluate.Evaluateshift
Evaluateshift(shiftRpt rpt, int sectionId)
{
 ShiftPI currentShiftI = DataService.GetEntity(GetDataRequest<ShiftPI>.Create(c =>
                c.ShiftID == rpt.ShiftID && c.SectionPanelID == sectionId, "ShiftPI"));

    if(currentShiftI != null)
    {
      List<SectionPanelPI> sectionPanelPIs = DataService.GetAll(GetDataRequest<SectionPanelHSEKPI>.Create(c =>
                        c.SectionPanelID == sectionId, "PI", "PIStatistics")).ToList();


                foreach(sectionPanelPI in sectionPanelPIs)
                {
                 do more stuff....
                }
    }
}


public void function2(publish p1,nextshift nx, List<publish> pub)
{
    foreach(publish in pub.ToList())
    {
    //do stuff
    }
}

public void function3(publish p1,nextshift nx, List<publish> pub)
{
    foreach(publish in pub.ToList())
    {
    //do stuff
    }
}

public void function4(publish p1,nextshift nx, List<publish> pub)
{
    foreach(publish in pub.ToList())
    {
    //do stuff
    }
}
dawncode
  • 578
  • 1
  • 8
  • 22
  • 1
    I guess your `//do stuff` modifies the collection like the exeption says. Provide more code. – t3chb0t Dec 08 '14 at 12:22
  • I have edited my code I've seen in other post about giving copy of the list by adding .ToList() – dawncode Dec 08 '14 at 12:47
  • ...now look inside `do more stuff`. There is one more list `sectionPanelPIs`. Do you modify this one? – t3chb0t Dec 08 '14 at 12:51
  • Is there another thread modifying `publishList` other than the ones we can see in your code? – Matthew Watson Dec 08 '14 at 13:07
  • these four functions are inside the top foreach loop. but I assume when I put Task.WaitAll it will not go out for next value unless and until all task get completed.. – dawncode Dec 08 '14 at 13:48
  • these four functions taking about 2 to 3 min each to complete the execution, that is why I want to execute them parallel. – dawncode Dec 08 '14 at 17:18

1 Answers1

0

The problem is that you are modifying the collection during it's enumeration: you're trying to add or remove some values, or replace values with new ones. This is not allowed. As I don't have the code of your foreach a can't suggest a proper sulotuin, but you can check this question: Collection was modified; enumeration operation may not execute

Community
  • 1
  • 1
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
  • yes i knew about adding .ToList() to list it will create new copy for processing but still i'm getting the error. – dawncode Dec 08 '14 at 12:48