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