我收到此消息“mscorlib.dll中出现'System.InvalidOperationException'类型的第一次机会异常”
但我不知道为什么会这样,这是我的代码:
//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();
}
}
异常详情:
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:
似乎在“foreach”的第一个循环中正确计算每一个,但在下一个循环中,当tempProcess减1时我得到了异常消息,请帮我解决这个问题。
编辑:添加了异常详细信息和固定名称切换但仍然发生