E
eBob.com
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
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