gpt4 book ai didi

c# - 为什么我会收到异常 InvalidOperationException?

转载 作者:太空狗 更新时间:2023-10-30 00:20:00 26 4
gpt4 key购买 nike

我编写了以下代码,从列表框中删除选定的值。我还想将其从字典列表中删除,然后将其更新/写入文本文件,这样当我再次运行程序并加载文本文件时,它将被更新,如果没有,它将在每次运行时继续显示已删除的项目再次申请。

private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
string sb;
if (e.KeyCode == Keys.Delete)
{
if (this.listBox1.SelectedIndex >= 0)
{
string obj = this.listBox1.SelectedValue.ToString();
data.Remove(obj);
listBox1.DataSource = null;
listBox1.DataSource = data;
foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)
{
for (int i = 0; i < kvp.Value.Count(); i++)
{
sb = "Url: " + kvp.Key + " --- " + "Local KeyWord: " + kvp.Value[i] + Environment.NewLine;
LocalyKeyWords.Remove(kvp.Key);
}
}

}
}

}

LocalyKeyWords 是一个字典>

在这种情况下,它包含两个项目/键,我删除了一个,我看到一个断点,这个已经被删除了。

问题是我是否需要删除 kvp.Key 或以某种方式删除我从 listBox 中删除的项目,这是我想从 LocalyKeywords 中删除的项目,它是 obj 变量,因为我正在这样做:

data.Remove(obj);

所以也许我还需要从 localyKeyWords 中删除 obj ?

我得到的错误是在它从 LocalyKeyWords 中删除项目后单击此行继续:

foreach (KeyValuePair<string, List<string>> kvp in LocalyKeyWords)

我收到错误:

集合已修改;枚举操作可能不会执行

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.Dictionary`2.Enumerator.MoveNext()
at GatherLinks.Form1.listBox1_KeyDown(Object sender, KeyEventArgs e) in d:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Form1.cs:line 959
at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at GatherLinks.Program.Main() in d:\C-Sharp\GatherLinks\GatherLinks\GatherLinks\Program.cs:line 18
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:

最佳答案

您正在从 LocalyKeyWords 中删除项目,同时正在对其进行迭代;正如异常消息所说,这是不允许的。

我不确定这里的大局是什么,但是本地化的解决方案是制作 LocalyKeyWords 的临时副本并对其进行迭代。然后,您可以毫不费力地修改“源”集合。

例子:

foreach (var kvp in LocalyKeyWords.ToList())  // .ToList() makes a temp copy

关于c# - 为什么我会收到异常 InvalidOperationException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15057712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com