Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Go Back  Xtreme Visual Basic Talk > > > Generic.List(Of T).ToArray exception - question


Reply
 
Thread Tools Display Modes
  #1  
Old 11-01-2014, 05:38 AM
jaccog's Avatar
jaccog jaccog is offline
Regular
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 96
Wink Generic.List(Of T).ToArray exception - question


Hi all,

I am getting an exception when the following line of code executes:
Code:
Dim keys() As String = RendererUpdateObjects.ToArray
The RendererUpdateObjects object is a Generic.list(Of string) and can be updated by another thread.

The exception is:
Quote:
System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.

So, yes, I am sure the exception is raised because another thread must be changing the list contents. On the other hand; I am just curious as to why this single method would cause this exception. I mean; the array is created based on the return value of the .ToArray function, right?
To my knowledge the .NET framework returns the array, then the Keys() array is resized to the size of the returned array and the returned array is then copied to the new Keys() array, right?

Just wondering....

Thanx!
__________________
Jacco.
-The one who says it cannot be done should never interrupt the one who is doing it-
Reply With Quote
  #2  
Old 11-01-2014, 10:34 AM
PlausiblyDamp's Avatar
PlausiblyDampGeneric.List(Of T).ToArray  exception - question PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You can get this exception if multiple threads are accessing the List structure, you will need to make sure the code is thread safe for this to work. As a starting point have a look at the SyncLock keyword, be warned however multi-threaded code can get very complicated very quickly.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 11-01-2014, 11:09 AM
jaccog's Avatar
jaccog jaccog is offline
Regular
 
Join Date: Mar 2005
Location: The Netherlands
Posts: 96
Default

Hi PlausiblyDamp,

Thanks for your answer. I did already fix it by using Synclock mechanisms. The thing is that I would not have expected this to happen as this 'ToArray' function seems like a single instruction to me (I know that the framework has some code behind it, but I assumed that that part would have been thread-safe).
__________________
Jacco.
-The one who says it cannot be done should never interrupt the one who is doing it-
Reply With Quote
  #4  
Old 11-01-2014, 12:40 PM
PlausiblyDamp's Avatar
PlausiblyDampGeneric.List(Of T).ToArray  exception - question PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

As a general rule assume things aren't thread-safe unless the documentation explicitly states that they are. Using the various synchronisation mechanisms comes with an associated performance cost, even in a single threaded environment so more often than not these things aren't thread safe.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
 
Generic.List(Of T).ToArray  exception - question
Generic.List(Of T).ToArray  exception - question
 
-->