【问题标题】:Visual Studio Crashes! - Collection was modified after the enumerator was instantiatedVisual Studio 崩溃! - 枚举器实例化后修改了集合
【发布时间】:2011-09-03 14:52:38
【问题描述】:

嘿,我有一个用户控件,它不断使我的 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];

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

所有线程都停止在同一行...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()

这是迄今为止我遇到的最奇怪的事情。 应该得到帮助。

【问题讨论】:

    标签: c# .net visual-studio-2010 debugging user-controls


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 你已经很接近了:P 将线程创建放置在 !DesignMode 兵营中并不是唯一的事情,我还放置了 While 循环并且它起作用了 :) Thx !
    【解决方案2】:

    您是否正在使用任何其他代码修改集合?当您在循环中枚举集合并尝试修改集合时,通常会发生这种情况。

    【讨论】:

    • 不,不修改任何东西......我只有几个运行几个线程的用户控件,也许是线程的问题?
    猜你喜欢
    • 2012-02-06
    • 2022-01-21
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多