473,320 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SyncLock and Array.SyncRoot and ReDim

I was changing some code in a multi-threaded application today and noticed
that it was not locking where it really needed to be locking. The Sub was
already working with an array so I just stuck a SyncLock ArrayName.SyncRoot
at the beginning of the Sub and an End SyncLock at the end. But this caused
the application to produce no output (an Excel spreadsheet)! After some
screwing around, sorry ... I mean experimenting, I noticed that the Sub
contained a ReDim Preserve for the array, i.e. ArrayName. When I first
noticed this I only wondered if that was thread safe, or if that was an
additional reason why this routine needed locking. (ArrayName is accessed
by all threads.) Then I thought, hmmm, if it is thread safe maybe it is
already using SyncRoot and maybe that is my problem. Changed the SyncLock
from SyncLock ArrayName.SyncRoot to SyncLock GetType(String) and all seems
to be well. (I'll have to come up with something better than
GetType(String).)

So I am wondering if it makes sense that you cannot use SyncLock
Array.SyncRoot around code which is doing a ReDim Preserve Array, or maybe
even just a ReDim Array? And, if so, does it make sense that you wouldn't
get an exception?

Bob
Oct 13 '08 #1
2 2923
"eBob.com" <eB******@totallybogus.comschrieb
I was changing some code in a multi-threaded application today and
noticed that it was not locking where it really needed to be
locking. The Sub was already working with an array so I just stuck
a SyncLock ArrayName.SyncRoot at the beginning of the Sub and an End
SyncLock at the end. But this caused the application to produce no
output (an Excel spreadsheet)! After some screwing around, sorry
... I mean experimenting, I noticed that the Sub contained a ReDim
Preserve for the array, i.e. ArrayName. When I first noticed this I
only wondered if that was thread safe, or if that was an additional
reason why this routine needed locking. (ArrayName is accessed by
all threads.) Then I thought, hmmm, if it is thread safe maybe it
is already using SyncRoot and maybe that is my problem. Changed the
SyncLock from SyncLock ArrayName.SyncRoot to SyncLock
GetType(String) and all seems to be well. (I'll have to come up
with something better than
GetType(String).)

So I am wondering if it makes sense that you cannot use SyncLock
Array.SyncRoot around code which is doing a ReDim Preserve Array, or
maybe even just a ReDim Array? And, if so, does it make sense that
you wouldn't get an exception?

ReDim [Preserve] always creates a new array. The lock is still on the old
array even if the new array has been assigned to the same variable that
pointed to the old array before. The lock is on the object, not on the
variable pointing to the object.
Armin

Oct 13 '08 #2
Bob,
In addition to Armin, try forever to avoid the Redim, I have the idea that
it is from the time of Basic 1.0 and absolute very inefficient to use.

Cor

"eBob.com" <eB******@totallybogus.comschreef in bericht
news:ux**************@TK2MSFTNGP03.phx.gbl...
>I was changing some code in a multi-threaded application today and noticed
that it was not locking where it really needed to be locking. The Sub was
already working with an array so I just stuck a SyncLock ArrayName.SyncRoot
at the beginning of the Sub and an End SyncLock at the end. But this
caused the application to produce no output (an Excel spreadsheet)! After
some screwing around, sorry ... I mean experimenting, I noticed that the
Sub contained a ReDim Preserve for the array, i.e. ArrayName. When I first
noticed this I only wondered if that was thread safe, or if that was an
additional reason why this routine needed locking. (ArrayName is accessed
by all threads.) Then I thought, hmmm, if it is thread safe maybe it is
already using SyncRoot and maybe that is my problem. Changed the SyncLock
from SyncLock ArrayName.SyncRoot to SyncLock GetType(String) and all seems
to be well. (I'll have to come up with something better than
GetType(String).)

So I am wondering if it makes sense that you cannot use SyncLock
Array.SyncRoot around code which is doing a ReDim Preserve Array, or maybe
even just a ReDim Array? And, if so, does it make sense that you wouldn't
get an exception?

Bob

Oct 14 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
3
by: Chris Dunaway | last post by:
I was using a Queue object like this to create my own specialized queue class for use with my own objects: Public Class MySpecializedQueue Private q As New Queue Public Sub Enqueue(obj As...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
2
by: j3ko | last post by:
Hi, I'm trying to start a thread that constantly iterates through an arraylist of items that the main thread adds and removes from...how would I accomplish this? Here's the gist of what I have:...
2
by: Fredrik Strandberg | last post by:
I have not been able to find the solution of this problem anywhere: I am building a class PrivateHelper that provides methods to access private members and invoke private methods, to be used for...
18
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point...
10
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
7
by: =?Utf-8?B?UmljYXJkbyBGdXJ0YWRv?= | last post by:
I have a big problem concerning arrays. I have an array of structs that is deply implemented in the code. If i change this implementation then i have to change everything, so after searching...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.