Collection was modified, enumeration operation may not execute

Topics: Help
Mar 30, 2012 at 6:35 PM

Hi radioman,

i have multithread application and i get this error.

************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at GMap.NET.WindowsForms.GMapOverlay.Render(Graphics g) in C:\Users\PATO\Desktop\GPS\WindowsFormsApplication1\GMap.NET.WindowsForms\GMap.NET.WindowsForms\GMapOverlay.cs:line 329
   at GMap.NET.WindowsForms.GMapControl.OnPaintOverlays(Graphics g) in C:\Users\PATO\Desktop\GPS\WindowsFormsApplication1\GMap.NET.WindowsForms\GMap.NET.WindowsForms\GMapControl.cs:line 1302
   at GMap.NET.WindowsForms.GMapControl.OnPaint(PaintEventArgs e) in C:\Users\PATO\Desktop\GPS\WindowsFormsApplication1\\GMap.NET.WindowsForms\GMap.NET.WindowsForms\GMapControl.cs:line 1192
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

I know why error occurs, because on one thread i read my collection and on another thread i modify collection. I was thinking to lock first loop but it's not work.

protected virtual void OnPaintOverlays(Graphics g)
      {
#if !PocketPC
         g.SmoothingMode = SmoothingMode.HighQuality;
#endif

         lock (Overlays)
         {
             foreach (GMapOverlay o in Overlays)
             {
                 if (o.IsVisibile)
                 {
                    o.Render(g);
                 }
             }
         }
...
...

have you ever met this problem?


Coordinator
Mar 30, 2012 at 6:49 PM

use backgroundworker or invoke

Dec 12, 2012 at 4:10 PM

i use backgroundworker but the error exist

Coordinator
Dec 12, 2012 at 5:38 PM

you use it in the wrong way

Dec 13, 2012 at 6:57 AM

 

the error does not happen always,notes that mistake:

1 application open

2 I click on a button I start the BackgroundWorker

3 The background is now working well, handles well objects without me blocking the application as you showed me you.

bkw the inserts new makers

sometimes it gives me the error other times it really flows

give me some advice where I'm wrong?

maybe I have to wait for a specific event, before making the start of bkw?

 

thank you for the support!!!

Coordinator
Dec 13, 2012 at 8:38 AM

insert/add/remove markers only in GUI thread

Dec 13, 2012 at 9:58 AM

 

What's that? I do not think I understand the operation

Dec 13, 2012 at 10:12 AM

 

I understand I should use in my bkw a delegate

Coordinator
Dec 13, 2012 at 12:54 PM

check the demo code, why you wanna go blind...