Visual Studio Crash! - The collection was changed after the counter instance was specified

Hi, I have a UserControl that has been crashing my Visual Studio all the time. So, I started another VS instance and debugged another VS, and here is what I realized:

Collection was modified after the enumerator was instantiated.

Here is my array:

    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
    };

And here is my loop that breaks the business

    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);
        }
    }

The line that Visual Studio constructor produces this.BackColor = colors[i];

Here is an image of working threads:

Threads

All threads are stopped on one line ... this.BackColor = colors[i];

Here is the EventViewer crash log :

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()

This is the strangest thing I have encountered so far. Please help me to find out.

+5
source share
2 answers

, , VS . , , , . , , Load, OnHandleCreated .. , , .

. , , . , dbase dbase . , , InvokeRequired , . :

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

, . , , , . , , . .

+3

, ? , .

0

All Articles