# Collection Modified Errors

**URL:** <https://community.thinkgeo.com/t/collection-modified-errors/320>\
**Category:** WinForms\
**Created:** [March 31, 2009, 8:14am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320 "2009-03-31T08:14:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [March 31, 2009, 8:14am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/1 "2009-03-31T08:14:00Z")

</div>

If&nbsp;I&nbsp;make&nbsp;several&nbsp;edits&nbsp;to&nbsp;layers&nbsp;concurrently&nbsp;without&nbsp;enough&nbsp;time&nbsp;for&nbsp;the&nbsp;previousoperation&nbsp;to&nbsp;complete,&nbsp;for&nbsp;instance&nbsp;removing&nbsp;layers&nbsp;one&nbsp;by&nbsp;one&nbsp;or&nbsp;move&nbsp;a&nbsp;layer&nbsp;up&nbsp;in&nbsp;the&nbsp;drawing&nbsp;order&nbsp;several&nbsp;times&nbsp;before&nbsp;the&nbsp;draw&nbsp;for&nbsp;the&nbsp;first&nbsp;action&nbsp;has&nbsp;completed,&nbsp;I&nbsp;receive&nbsp;a&nbsp;Collection&nbsp;modified&nbsp;error.&nbsp;Is&nbsp;there&nbsp;a&nbsp;way&nbsp;to&nbsp;avoid&nbsp;this?

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [April 2, 2009, 3:12am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/2 "2009-04-02T03:12:54Z")

</div>

Nelson,&nbsp;  
&nbsp;&nbsp;  
&nbsp;A&nbsp;common&nbsp;way&nbsp;to&nbsp;avoid&nbsp;the&nbsp;cross&nbsp;threading&nbsp;issue&nbsp;is&nbsp;by&nbsp;using&nbsp;lock&nbsp;(SyncLock&nbsp;in&nbsp;[VB.NET](http://VB.NET)).&nbsp;For&nbsp;example,&nbsp;whenever&nbsp;you&nbsp;are&nbsp;going&nbsp;to&nbsp;do&nbsp;some&nbsp;operations&nbsp;on&nbsp;the&nbsp;collections,&nbsp;just&nbsp;lock&nbsp;one&nbsp;object.&nbsp;And&nbsp;whenever&nbsp;you&nbsp;are&nbsp;going&nbsp;to&nbsp;draw&nbsp;the&nbsp;map,&nbsp;lock&nbsp;the&nbsp;same&nbsp;object.&nbsp;In&nbsp;this&nbsp;way,&nbsp;the&nbsp;collection&nbsp;operation&nbsp;(remove&nbsp;a&nbsp;layer&nbsp;or&nbsp;move&nbsp;a&nbsp;layer&nbsp;up)&nbsp;will&nbsp;not&nbsp;be&nbsp;start&nbsp;until&nbsp;the&nbsp;drawing&nbsp;is&nbsp;finished,&nbsp;and&nbsp;vice&nbsp;verse.&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;To&nbsp;lock&nbsp;one&nbsp;object&nbsp;while&nbsp;drawing,&nbsp;you&nbsp;can&nbsp;simply&nbsp;create&nbsp;a&nbsp;new&nbsp;FeatureLayer,&nbsp;override&nbsp;the&nbsp;DrawCore&nbsp;method&nbsp;and&nbsp;check&nbsp;the&nbsp;lock&nbsp;root&nbsp;for&nbsp;it.&nbsp;Pass&nbsp;in&nbsp;the&nbsp;object&nbsp;you&nbsp;want&nbsp;to&nbsp;lock&nbsp;to&nbsp;LockRoot&nbsp;and&nbsp;it&nbsp;will&nbsp;work.&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;`Class LockedFeatureLayer     Inherits FeatureLayer Private _LockRoot As Object     Public Property LockRoot() As Object         Get             Return _LockRoot         End Get         Set(ByVal value As Object)             _LockRoot = value         End Set     End Property         Protected Overloads Overrides Sub DrawCore(ByVal canvas As GeoCanvas, ByVal labelsInAllLayers As Collection(Of SimpleCandidate))         SyncLock LockRoot             MyBase.DrawCore(canvas, labelsInAllLayers)         End SyncLock     End Sub End Class `&nbsp;  
&nbsp;Finally,&nbsp;just&nbsp;remind&nbsp;you&nbsp;usually&nbsp;want&nbsp;to&nbsp;inherit&nbsp;from&nbsp;a&nbsp;concrete&nbsp;FeatureLayer&nbsp;like&nbsp;ShapeFileFeatureLayer&nbsp;or&nbsp;PostgreFeatureLayer,&nbsp;the&nbsp;code&nbsp;above&nbsp;is&nbsp;just&nbsp;showing&nbsp;the&nbsp;idea.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Ben

---

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [April 2, 2009, 6:59am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/3 "2009-04-02T06:59:22Z")

</div>

Ok&nbsp;I'll&nbsp;look&nbsp;into&nbsp;this.&nbsp;I&nbsp;realize&nbsp;Map&nbsp;Suite&nbsp;utilizes&nbsp;multi-threading&nbsp;so&nbsp;I'm&nbsp;assuming&nbsp;that&nbsp;is&nbsp;why&nbsp;you&nbsp;are&nbsp;suggesting&nbsp;this&nbsp;work&nbsp;aroudn&nbsp;but&nbsp;I&nbsp;just&nbsp;wanted&nbsp;to&nbsp;clarify&nbsp;that&nbsp;you&nbsp;didn't&nbsp;think&nbsp;I&nbsp;was&nbsp;necessarily&nbsp;utilizing&nbsp;multi-threading&nbsp;in&nbsp;my&nbsp;application&nbsp;and&nbsp;that&nbsp;may&nbsp;be&nbsp;why&nbsp;I&nbsp;was&nbsp;getting&nbsp;the&nbsp;above&nbsp;error,&nbsp;because&nbsp;I'm&nbsp;not.

Thanks&nbsp;for&nbsp;the&nbsp;suggestion.

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [April 3, 2009, 12:27am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/4 "2009-04-03T00:27:57Z")

</div>

Nelson,&nbsp;  
&nbsp;&nbsp;  
&nbsp;In&nbsp;fact,&nbsp;the&nbsp;current&nbsp;Desktop&nbsp;Edition&nbsp;3.0.199&nbsp;doesn’t&nbsp;have&nbsp;the&nbsp;MutliThreading&nbsp;turned&nbsp;on&nbsp;by&nbsp;default&nbsp;(because&nbsp;it&nbsp;has&nbsp;some&nbsp;cross&nbsp;threading&nbsp;issue)&nbsp;and&nbsp;I&nbsp;thought&nbsp;you&nbsp;are&nbsp;of&nbsp;course&nbsp;using&nbsp;the&nbsp;multi-threading&nbsp;yourself&nbsp;which&nbsp;leads&nbsp;to&nbsp;the&nbsp;problem.&nbsp;Seems&nbsp;I’m&nbsp;wrong&nbsp;then.&nbsp;So&nbsp;now&nbsp;can&nbsp;you&nbsp;let&nbsp;me&nbsp;know&nbsp;how&nbsp;to&nbsp;recreate&nbsp;this&nbsp;issue&nbsp;with&nbsp;single&nbsp;thread?&nbsp;As&nbsp;you&nbsp;mentioned&nbsp;“make&nbsp;several&nbsp;edits&nbsp;to&nbsp;layers&nbsp;concurrently”&nbsp;and&nbsp;“move&nbsp;a&nbsp;layer&nbsp;up&nbsp;&nbsp;…&nbsp;before&nbsp;the&nbsp;draw&nbsp;&nbsp;…&nbsp;has&nbsp;completed”,&nbsp;just&nbsp;not&nbsp;very&nbsp;sure&nbsp;what&nbsp;does&nbsp;that&nbsp;exactly&nbsp;mean&nbsp;with&nbsp;single&nbsp;thread,&nbsp;can&nbsp;you&nbsp;provide&nbsp;a&nbsp;piece&nbsp;of&nbsp;code&nbsp;for&nbsp;that?&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Ben

---

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [April 3, 2009, 6:35am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/5 "2009-04-03T06:35:13Z")

</div>

Sorry,&nbsp;I&nbsp;should&nbsp;have&nbsp;been&nbsp;more&nbsp;clear.&nbsp;I&nbsp;have&nbsp;a&nbsp;tree&nbsp;view&nbsp;listing&nbsp;the&nbsp;layers&nbsp;loaded&nbsp;in&nbsp;the&nbsp;map&nbsp;with&nbsp;an&nbsp;add&nbsp;and&nbsp;delete&nbsp;button.&nbsp;If&nbsp;i&nbsp;go&nbsp;one&nbsp;by&nbsp;one&nbsp;and&nbsp;delete&nbsp;the&nbsp;layers&nbsp;quickly,&nbsp;the&nbsp;map&nbsp;still&nbsp;seems&nbsp;to&nbsp;be&nbsp;in&nbsp;the&nbsp;process&nbsp;of&nbsp;disposing&nbsp;data&nbsp;and&nbsp;what&nbsp;not&nbsp;and&nbsp;gives&nbsp;me&nbsp;a&nbsp;collection&nbsp;modified&nbsp;error.&nbsp;It&nbsp;does&nbsp;not&nbsp;point&nbsp;to&nbsp;a&nbsp;line&nbsp;in&nbsp;code&nbsp;where&nbsp;it&nbsp;takes&nbsp;place.&nbsp;  
&nbsp;&nbsp;  
&nbsp;{"Collection&nbsp;was&nbsp;modified;&nbsp;enumeration&nbsp;operation&nbsp;may&nbsp;not&nbsp;execute."}&nbsp;  
&nbsp;Data:&nbsp;{System.Collections.ListDictionaryInternal}&nbsp;

---

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [April 3, 2009, 6:35am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/6 "2009-04-03T06:35:20Z")

</div>

Sorry,&nbsp;I&nbsp;should&nbsp;have&nbsp;been&nbsp;more&nbsp;clear.&nbsp;I&nbsp;have&nbsp;a&nbsp;tree&nbsp;view&nbsp;listing&nbsp;the&nbsp;layers&nbsp;loaded&nbsp;in&nbsp;the&nbsp;map&nbsp;with&nbsp;an&nbsp;add&nbsp;and&nbsp;delete&nbsp;button.&nbsp;If&nbsp;i&nbsp;go&nbsp;one&nbsp;by&nbsp;one&nbsp;and&nbsp;delete&nbsp;the&nbsp;layers&nbsp;quickly,&nbsp;the&nbsp;map&nbsp;still&nbsp;seems&nbsp;to&nbsp;be&nbsp;in&nbsp;the&nbsp;process&nbsp;of&nbsp;disposing&nbsp;data&nbsp;and&nbsp;what&nbsp;not&nbsp;and&nbsp;gives&nbsp;me&nbsp;a&nbsp;collection&nbsp;modified&nbsp;error.&nbsp;It&nbsp;does&nbsp;not&nbsp;point&nbsp;to&nbsp;a&nbsp;line&nbsp;in&nbsp;code&nbsp;where&nbsp;it&nbsp;takes&nbsp;place.&nbsp;  
&nbsp;&nbsp;  
&nbsp;{"Collection&nbsp;was&nbsp;modified;&nbsp;enumeration&nbsp;operation&nbsp;may&nbsp;not&nbsp;execute."}&nbsp;  
&nbsp;Data:&nbsp;{System.Collections.ListDictionaryInternal}&nbsp;

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [April 8, 2009, 5:32am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/7 "2009-04-08T05:32:32Z")

</div>

Nelson,&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;guess&nbsp;after&nbsp;removing&nbsp;a&nbsp;node&nbsp;in&nbsp;the&nbsp;tree&nbsp;view,&nbsp;&nbsp;you&nbsp;remove&nbsp;the&nbsp;corresponding&nbsp;layer&nbsp;and&nbsp;redraw&nbsp;the&nbsp;map.&nbsp;Just&nbsp;wondering&nbsp;did&nbsp;the&nbsp;application&nbsp;get&nbsp;“No&nbsp;Response”&nbsp;before&nbsp;the&nbsp;map&nbsp;finished&nbsp;drawing?&nbsp;If&nbsp;only&nbsp;use&nbsp;Single&nbsp;Thread,&nbsp;I&nbsp;think&nbsp;the&nbsp;application&nbsp;will&nbsp;stun&nbsp;here&nbsp;and&nbsp;will&nbsp;only&nbsp;be&nbsp;available&nbsp;after&nbsp;drawing&nbsp;is&nbsp;completed.&nbsp;  
&nbsp;&nbsp;  
&nbsp;If&nbsp;you&nbsp;are&nbsp;using&nbsp;one&nbsp;thread&nbsp;for&nbsp;the&nbsp;UI&nbsp;and&nbsp;the&nbsp;other&nbsp;thread&nbsp;for&nbsp;the&nbsp;other&nbsp;parts,&nbsp;&nbsp;you&nbsp;can&nbsp;try&nbsp;the&nbsp;“lock”&nbsp;statements&nbsp;above,&nbsp;which&nbsp;may&nbsp;solve&nbsp;the&nbsp;problem.&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Ben

---

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [April 9, 2009, 6:18am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/8 "2009-04-09T06:18:11Z")

</div>

Yes,&nbsp;I&nbsp;am&nbsp;removing&nbsp;the&nbsp;layers&nbsp;from&nbsp;a&nbsp;TreeView&nbsp;which&nbsp;in&nbsp;that&nbsp;same&nbsp;routine&nbsp;removes&nbsp;the&nbsp;layer&nbsp;from&nbsp;the&nbsp;map.&nbsp;Actually,&nbsp;two&nbsp;maps;&nbsp;A&nbsp;locus&nbsp;map&nbsp;and&nbsp;the&nbsp;main&nbsp;map.&nbsp;&nbsp;  
&nbsp;&nbsp;  
&nbsp;I&nbsp;am&nbsp;not&nbsp;building&nbsp;multi-threading&nbsp;into&nbsp;the&nbsp;application&nbsp;nor&nbsp;have&nbsp;I&nbsp;manually&nbsp;turned&nbsp;on&nbsp;multi-threading&nbsp;on&nbsp;the&nbsp;map&nbsp;control.&nbsp;I&nbsp;do&nbsp;not&nbsp;get&nbsp;a&nbsp;No&nbsp;Response.&nbsp;If&nbsp;the&nbsp;map&nbsp;is&nbsp;ever&nbsp;not&nbsp;finished&nbsp;drawing,&nbsp;I&nbsp;can&nbsp;continue&nbsp;to&nbsp;pan&nbsp;and&nbsp;zoom&nbsp;just&nbsp;fine.&nbsp;This&nbsp;is&nbsp;why&nbsp;I&nbsp;thought&nbsp;maybe&nbsp;there&nbsp;was&nbsp;some&nbsp;slight&nbsp;multi-threading&nbsp;in&nbsp;use&nbsp;in&nbsp;the&nbsp;component.&nbsp;  
&nbsp;&nbsp;  
&nbsp;But&nbsp;as&nbsp;I&nbsp;mentioned&nbsp;earlier,&nbsp;if&nbsp;I&nbsp;add&nbsp;layers&nbsp;rapidly&nbsp;through&nbsp;a&nbsp;dialog&nbsp;box&nbsp;or&nbsp;remove&nbsp;them&nbsp;rapidly&nbsp;through&nbsp;the&nbsp;tree&nbsp;view&nbsp;or&nbsp;button&nbsp;then&nbsp;this&nbsp;type&nbsp;of&nbsp;error&nbsp;occurs.&nbsp;

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [April 10, 2009, 4:01am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/9 "2009-04-10T04:01:12Z")

</div>

&nbsp;

Nelson,&nbsp;

It's&nbsp;strange&nbsp;that&nbsp;in&nbsp;our&nbsp;test,&nbsp;the&nbsp;app&nbsp;had&nbsp;no&nbsp;response&nbsp;before&nbsp;finished&nbsp;drawing.&nbsp;Could&nbsp;you&nbsp;please&nbsp;send&nbsp;a&nbsp;simple&nbsp;demo&nbsp;for&nbsp;us&nbsp;to&nbsp;recreate?&nbsp;Here&nbsp;is&nbsp;our&nbsp;test&nbsp;sample,&nbsp;please&nbsp;let&nbsp;me&nbsp;know&nbsp;what's&nbsp;the&nbsp;difference&nbsp;between&nbsp;yours.

Thanks,

Ben&nbsp;

[574-Demo.zip](https://community.thinkgeo.com/uploads/default/original/2X/d/db060695b261aee6fee9e2f992dce83c79f736d2.zip) (10.1 KB)

---

<div class="post-metadata">

**Author:** ![Nelson](https://community.thinkgeo.com/letter_avatar_proxy/v4/letter/n/dec6dc/32.png) [@Nelson](https://community.thinkgeo.com/u/Nelson)\
**Post date:** [April 10, 2009, 10:31am UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/10 "2009-04-10T10:31:29Z")

</div>

With&nbsp;the&nbsp;new&nbsp;assemblies&nbsp;sent&nbsp;via&nbsp;email,&nbsp;the&nbsp;behavior&nbsp;you&nbsp;describe&nbsp;is&nbsp;true.&nbsp;I&nbsp;have&nbsp;incorporated&nbsp;some&nbsp;of&nbsp;the&nbsp;new&nbsp;features&nbsp;into&nbsp;my&nbsp;viewer&nbsp;now&nbsp;so&nbsp;I&nbsp;can't&nbsp;or&nbsp;would&nbsp;like&nbsp;not&nbsp;to&nbsp;roll&nbsp;back&nbsp;to&nbsp;verify.

In&nbsp;this&nbsp;way&nbsp;it&nbsp;is&nbsp;evident&nbsp;it&nbsp;is&nbsp;running&nbsp;in&nbsp;a&nbsp;single&nbsp;thread&nbsp;mode&nbsp;and&nbsp;no&nbsp;error&nbsp;occurs.&nbsp;Indeed,&nbsp;the&nbsp;window&nbsp;does&nbsp;hang&nbsp;as&nbsp;the&nbsp;operation&nbsp;completes.

Thank&nbsp;you.

---

<div class="post-metadata">

**Author:** ![Ben](https://community.thinkgeo.com/user_avatar/community.thinkgeo.com/ben/32/25720_2.png) [@Ben](https://community.thinkgeo.com/u/Ben)\
**Post date:** [April 12, 2009, 10:29pm UTC](https://community.thinkgeo.com/t/collection-modified-errors/320/11 "2009-04-12T22:29:54Z")

</div>

Nelson,&nbsp;  
&nbsp;&nbsp;  
&nbsp;That’s&nbsp;fine.&nbsp;Let’s&nbsp;refocus&nbsp;on&nbsp;that&nbsp;when&nbsp;you&nbsp;encountered&nbsp;it&nbsp;again.&nbsp;  
&nbsp;&nbsp;  
&nbsp;Thanks,&nbsp;  
&nbsp;&nbsp;  
&nbsp;Ben.
