c # Object in use and Threads


I have been fighting for several days with a problem of

  • Modified collection; the operation of enumeration.
  • Object in use
  • the discarded object can not be accessed
  • I have the following code in the main form (which modifies a GMAPControler object in the form):

        // RE-DRAW MAP
        public void reloadMapOverlay(GMapOverlay overlay)
                Invoke(new Action(() => this.map_Box.Overlays.Add(overlay)));
            catch (Exception e)
                Console.WriteLine("reloadMapOverlay: {0}", e);
                this.setError("reloadMapOverlay: " + e);

    and in another class (Map_Custom.cs) I call the previous function:


    (addRoute is called by a thread:

                      Thread addRoute = new Thread(new ThreadStart(map.addRoute));
                        addRoute.IsBackground = true;


    • Errors jumps me in the overlay object.

    pd: doing tests, if in

     Invoke(new Action(() => this.map_Box.Overlays.Add(overlay)));

    I put

     Invoke(new Action(() => this.map_Box.Overlays.Add(new GMapOverlay("A"))));

    I have no failures, but of course, the thing is to pass an overlay with data and not create a new one every time.

    a greeting.

    asked by 19.05.2016 в 11:56

    2 answers


    Considering that the procedure is asynchronous, it is very likely that when the thread starts, the object is no longer in memory, the solution is to clone it .clone () or build a new object based on what you want to pass as a parameter .


    Another solution is to have an attribute that contains that object that you want to pass, at the class level that executes the thread.

    The above is because an object is never passed by value, in .NET the objects are always passed by reference therefore if an object is created at the function level and that function ends, when the thread will look for that point in memory, you can not find anything.

    answered by 19.05.2016 в 15:05
    reloadMapPosition: System.InvalidOperationException: Colección modificada; puede que no se ejecute la operación de enumeración. en System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) en System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) en System.Windows.Forms.Control.Invoke(Delegate method) en Car_Controller.View.FormMain.reloadMapPosition() en c:\Users\Gon\Desktop\Car_Controller\Car_Controller\Views\FormMain.cs:línea 903

    This is a concurrency problem.

    Where do you create the map object? It looks like you are modifying / deleting it in some way before the completion of the addRoute thread execution.

    Try one thing, insert a simple


    just after the


    If that works for you properly, then it's happening to you that I tell you. The simplest solution that I can think of in this case is to pass it a copy of the map object, instead of passing it the map.addRoute reference.

    I hope it helps you.

    answered by 23.05.2016 в 17:51