Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:29:39+00:00 2026-05-25T11:29:39+00:00

Hey i have a UserControl that kept crashing my Visual Studio. So i ran

  • 0

Hey i have a UserControl that kept crashing my Visual Studio.
So i ran another instance of VS and debugged the other VS and this is what i figured:

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 crashes the bussines

    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 crashes the Visual Studio Designer is this.BackColor = colors[i];

Here is the image of the running Threads:

Threads

All threads stopped on the same 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 weirdest thing i encountered so far.
Help whould be appriciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:29:39+00:00Added an answer on May 25, 2026 at 11:29 am

    As you found out, your code is crashing the designer, taking VS down with it. The problem is that you start a thread in design mode, triggered by the designer running some of your code at design time. It for example will run the constructor, the Load event, OnHandleCreated, etcetera. That makes for a very nice design-time experience, your control will look just like it does at runtime.

    But that can also cause plenty of problems. You have to avoid running code that may cause an exception when it runs in a different execution context. Classic examples are trying to open a file without specifying the full path, opening a dbase connection with the dbase server offline or unreachable. And definitely starting a thread, InvokeRequired is not going to reliably work as the designer constructs and destroys the native window handle. The fix is simple:

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

    You’ll need to do more work, this code won’t work well at runtime either. The threaded code will bomb when the form on which the user control is placed is closed. Once you fix that, odds are good that it now works correctly at design-time as well. But don’t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I have this code that sends an email with some data sent by
Hey; I have the following visual hyerarchy in a usercontrol: Grid -> Canvas ->
Hey i have this div that shows as a popup: <div class=popup> </div> Then
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
Hey I have a really annoying IE7 bug that I am trying to work
Hey I have this code but it doesn't work because it is expecting a
Hey we have a library class (lib/Mixpanel) that calls delayed job as follows: class
Hey. I have a tabcontrol that is bound to an observable collection. I've tried
Hey I have this kinda structure in my ini file I want to be
hey there i am having problem i have List<List<memoryCard>> that i want to show

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.