1

I got this message "A first chance exception of type 'System.InvalidOperationException' occurred in mscorlib.dll"

but I have no idea why this is happening, here is my code:

    //declarations
    private List<Proces> tempProcess;
    private List<Proces> NotScheduled;
    private List<Proces> kolejka;
    private List<Proces> pokolenie;
    private List<float> licznikLB;


//function i have problem with
    protected override void bnbalg()
    {

        foreach (var proces in NotScheduled)
        {
            tempProcess= NotScheduled;
            tempProcess.RemoveAt(0);
            float LB = CalculateLB(tempProcess);
            if (proces.TerminZakonczenia < LB)
            {
                pokolenie.Add(proces);
                licznikLB.Add(LB);
            }
        }
        kolejka.Add(pokolenie[licznikLB.IndexOf(licznikLB.Min())]);
        NotScheduled.Remove(pokolenie[licznikLB.IndexOf(licznikLB.Min())]);
        tempProcess.Clear();
        pokolenie.Clear();
        licznikLB.Clear();
        if (NotScheduled.Any())
        {
            bnbalg();
        }

    }

exception details:

        System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=Collection was modified; enumeration operation may not execute.
  Source=mscorlib
  StackTrace:
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at Zssk.bnb.bnbalg() in c:\Users\TeTorro\Documents\GitHub\zssk\Zssk\bnb.cs:line 115
   at Zssk.bazabnb.WykonajProcesy(Procesor procesor, IEnumerable`1 procesy) in c:\Users\TeTorro\Documents\GitHub\zssk\Zssk\bazabnb.cs:line 60
   at Zssk.Miernik.Pomiar(IAlgorytm algorytm, Int32 liczbaPomiarow, String nazwaPliku) in c:\Users\TeTorro\Documents\GitHub\zssk\Zssk\Miernik.cs:line 61
   at Zssk.Program.<>c__DisplayClass2.<Main>b__1(IAlgorytm x) in c:\Users\TeTorro\Documents\GitHub\zssk\Zssk\Program.cs:line 27
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at Zssk.Program.Main(String[] args) in c:\Users\TeTorro\Documents\GitHub\zssk\Zssk\Program.cs:line 27
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

It seems to calculate everyting correctly in the first loop of "foreach", but in the next loop, when tempProcess is decreased by one i got the exception message, please help me solve this one.

EDIT: added exception details and fixed name switch but still happens

Akka Jaworek
  • 1,970
  • 4
  • 21
  • 47
  • 1
    Doesn't look correct as you initialise `cnt = 0` within the loop and increment it at the end of the loop. Also not a good idea to manipulate the List you're iterating over. – phuzi May 06 '15 at 21:30
  • 3
    Modifying a collection while enumerating over it doesn't seems to be a wise move. And if you look better at your inner exception you should find that also the runtime doesn't like it too much – Steve May 06 '15 at 21:30
  • i just figured out i failed hard and switched names NotScheduled and tempProcess – Akka Jaworek May 06 '15 at 21:32
  • 1
    When you post a question about a exception please click [copy exception detail to clipboard](http://blogs.msdn.com/b/saraford/archive/2008/08/07/did-you-know-you-can-copy-the-exception-details-with-one-click-from-the-exception-assistant-276.aspx) then post the contents of what it copied in to your question. EDIT: and from your last comment, you are still going to get errors for modifying a collection while inside a `foreach`. – Scott Chamberlain May 06 '15 at 21:32
  • @AkkaJaworek from what I can see in this code you don't need this `tempProcess.RemoveAt(cnt);`, instead, set a flag in your Proces class that exclude the instance from your following call to CalculateLB` – Steve May 06 '15 at 21:36

0 Answers0