473,809 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.SyncR oot
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.SyncR oot 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 2943
"eBob.com" <eB******@total lybogus.comschr ieb
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.SyncR oot 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.SyncR oot 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******@total lybogus.comschr eef in bericht
news:ux******** ******@TK2MSFTN GP03.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.SyncR oot
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.SyncR oot 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
39476
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 rst.EOF i = i + 1 If rst!Something = Then astrMyArray(i) = rst!Something
4
2829
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 that I can preform complicated operations against this data. The returned record count in these operations is always variable. 1. I have been using an arraylist.add function to handle non-multidemional returns but was wondering if I'm better...
3
4798
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 MyCustomClass) SyncLock q.SyncRoot q.Enqueue(obj)
19
3159
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 than in .Net environment, under VS 2005 Beta 2. Does anyone have any idea whether this will be addressed in the final release? Thanks, Tomasz
2
1747
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: (can anybody give me pointers on what i'm doing wrong?) Class SecondThread aList as new arraylist innerthread as new thread(addressof task)
2
2072
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 testing. In a particular test case, an array is passed to the Method Under Test, and then that MUT performs a ReDim on the array. The test code then needs to check the array, but the effect of the ReDim statement does not propagate to the test...
18
3259
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 storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
10
12215
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 help do the most basic things. One of the "basic" things I haven't been able to find out how to do is how to delete an item from a multidimensional array object and resize it afterwards. It seems so easy to conceive of the code to delete...
7
1137
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 google and a lot of newsgroups i came up with this question: With the following array, how can i make a search without having to use a loop to seek for a certain "thing" in the array of structs? (i've tryed to use array.find(), but because its an array...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10375
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.