gpt4 book ai didi

c# - Visual Studio 崩溃! - 在实例化枚举器后修改了集合

转载 作者:太空狗 更新时间:2023-10-29 23:33:59 25 4
gpt4 key购买 nike

嘿,我的 UserControl 一直让我的 Visual Studio 崩溃。所以我运行了另一个 VS 实例并调试了另一个 VS,这就是我的想法:

Collection was modified after the enumerator was instantiated.

这是我的数组:

    private static Color[] colors = 
{
Color.FromArgb(155, 188, 255), // 40000
Color.FromArgb(156, 189, 255), // 39500
Color.FromArgb(157, 188, 255), // 39000
Color.FromArgb(156, 189, 254), // 38500
};

这是使业务崩溃的循环

    public Heater()
{
InitializeComponent();
this.tarTemp = this.curTemp;
new Thread(() => UpdateTemp(true)).Start();
}

private delegate void UpdateTempDelegate(bool loop);
private void UpdateTemp(bool loop)
{
if (lblTemp.InvokeRequired)
{
UpdateTempDelegate del = new UpdateTempDelegate(UpdateTemp);
lblTemp.Invoke(del, loop);
}
else
{
do
{
lblTemp.Text = curTemp + C;
if (curTemp >= 0)
{
int i = curTemp - 10;
if (i < 0)
i = 0;
if (i > colors.Length - 1)
i = colors.Length - 1;
this.BackColor = colors[i]; // I'M CRASHING !!!
}
} while (loop && !this.Disposing);
}
}

使 Visual Studio 设计器崩溃的行是 this.BackColor = colors[i];

这是正在运行的线程的图像:

Threads

所有线程都停止在同一行... this.BackColor = colors[i];

这是EventViewer崩溃日志:

Application: devenv.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.InvalidOperationException
Stack:
at System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource)
at System.Collections.Generic.SortedList`2+SortedListValueEnumerator[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MoveNext()
at Microsoft.VisualStudio.Shell.ServiceProviderHierarchy.GetService(System.Type)
at System.ComponentModel.Design.ServiceContainer.GetService(System.Type)
at System.ComponentModel.Design.DesignerHost.GetService(System.Type)
at System.ComponentModel.Design.DesignerHost+Site.System.IServiceProvider.GetService(System.Type)
at System.Windows.Forms.Control.get_AmbientPropertiesService()
at System.Windows.Forms.Control.get_BackColor()
at System.Windows.Forms.Control.set_BackColor(System.Drawing.Color)
at Multiplier.Heater.UpdateTemp(Boolean)
at Multiplier.Heater.<.ctor>b__0()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()

这是我迄今为止遇到的最奇怪的事情。需要帮助的人。

最佳答案

如您所见,您的代码使设计器崩溃,VS 随之崩溃。问题是你在设计模式下启动一个线程,由设计者在设计时运行你的一些代码触发。例如,它将运行构造函数、Load 事件、OnHandleCreated 等。这带来了非常好的设计时体验,您的控件看起来就像在运行时一样。

但这也会导致很多问题。您必须避免运行在不同执行上下文中运行时可能导致异常的代码。经典示例是尝试在不指定完整路径的情况下打开文件,打开与数据库服务器脱机或无法访问的数据库连接。并且肯定会启动一个线程,InvokeRequired 不会在设计人员构造和销毁 native 窗口句柄时可靠地工作。修复很简单:

public Heater()
{
InitializeComponent();
this.tarTemp = this.curTemp;
if (!this.DesignMode) {
new Thread(() => UpdateTemp(true)).Start();
}
}

您需要做更多的工作,此代码在运行时也无法正常工作。当放置用户控件的窗体关闭时,线程代码将爆炸。一旦你解决了这个问题,它现在在设计时也能正常工作的可能性很大。但是不要。

关于c# - Visual Studio 崩溃! - 在实例化枚举器后修改了集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7293846/

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