Click here to Skip to main content
15,924,367 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: HowTo Bind An ImageButton to a DataSource.Help!!! Pin
Dave Kreskowiak22-Apr-05 5:14
mveDave Kreskowiak22-Apr-05 5:14 
Generali Pin
Anonymous22-Apr-05 4:09
Anonymous22-Apr-05 4:09 
GeneralRe: i Pin
toxcct22-Apr-05 4:32
toxcct22-Apr-05 4:32 
Generalcollections doubt Pin
carlos_rocha22-Apr-05 3:56
carlos_rocha22-Apr-05 3:56 
GeneralRe: collections doubt Pin
Dave Kreskowiak22-Apr-05 5:12
mveDave Kreskowiak22-Apr-05 5:12 
GeneralRe: collections doubt Pin
carlos_rocha22-Apr-05 7:42
carlos_rocha22-Apr-05 7:42 
GeneralRe: collections doubt Pin
Dave Kreskowiak22-Apr-05 13:17
mveDave Kreskowiak22-Apr-05 13:17 
GeneralRe: collections doubt Pin
rwestgraham22-Apr-05 14:11
rwestgraham22-Apr-05 14:11 
You need to do two things:

1) Ensure that no other thread is actively modifying the collection or any of it's members.

2) Ensure that no other thread is currently reading any collection members since collection access is not inherently threadsafe.

MSDN does a generally poor job of describing thread synchronization in VB.NET. Their examples explain just enough to get you into trouble, mostly.

The problem is a synckLock, aka critical section, will only protect a section of code. If you put a critical section on, for example, the collection Delete method this will ensure that only one thread can delete a collection member at any given time, but it provides no guarantees that other threads are not currently enumerating the collection, so you would still get errors.

The only way you could accomplish thread safe access using a syncLock (aka critical section) with a collection is if you put the syncLock on the enumeration code, AND you ONLY modify a collection or it's members within the For ..Next loop.

This has two very obnoxious side effects:

Of course, only allowing write access within a For .. Each enumeration results in awkward and inefficient code.

All other threads are blocked when a single thread accesses the collection for any reason. What you really want is to only lock the collection when one thread needs to modify it.

So syncLock really is not suitable for this scenario. The correct way to do this is to implement locking if and only if a thread needs to modify the collection.

I'll explain how to do this the old-fashioned way with mutexes and semaphores.

The following procedure will ensure thread safe collection usage with minimal unneccessary locking.

Execute the following steps to "write" protect a collection, i.e. when a thread intends to access the collection for modification.

1) When a thread wishes to modify the collection or it's members it must first acquire a mutex. The mutex is only available to one thread at any given time and is used to block all other threads from either read or write access to the collection.

2) After the "writer" thread acquires the mutex, it must wait until all other threads have released their semaphores which occurs when all other threads which are actively accessing the collection as "readers" have finished.

3) When all semaphores are signalled, the "writer" is ensured it is sole access to the collection and the collection or any of it's members may safely be modified because all other threads are now blocked from read or write access.

4) When the writer is done modifying the collection or any of it's members, it releases the mutex, which unblocks all other threads.

Execute the following steps before any thread accesses the collection for read purposes only.

1) When a thread wishes to read the collection by either item access or enumeration it must first acquire a mutex. If the thread cannot acquire the mutex, it means another thread is possibly writing to the collection, so it must wait.

2) After the "reader" thread acquires the mutex, it should acquire a semaphore to signal to any subsequent writers that a thread is accessing the collection for reads.

3) Release the mutex so the next reader may acquire it long enough to get the next semaphore.

4) When the reader is done accessing the collection or any of it's members, it releases the semaphore.

You should implement all the above actions in a class that wraps your collection and provides thread-safe access to it.

VB.NET provides a ReaderWriterLock class that essentially implements the same operations I describe above behind the scenes. But I suggest you use the Win32 APIs to create your own mutex/semaphore code for synchronization so you can learn how it really works. VB.NET wrappers are fine if you understand threading synchronization fundamentals already. If you don't, you are a lot better off working directly with Win32 primitives.

Robert
GeneralRe: collections doubt Pin
carlos_rocha25-Apr-05 22:31
carlos_rocha25-Apr-05 22:31 
GeneralRe: collections doubt Pin
rwestgraham26-Apr-05 13:09
rwestgraham26-Apr-05 13:09 
GeneralRe: collections doubt Pin
carlos_rocha26-Apr-05 22:19
carlos_rocha26-Apr-05 22:19 
GeneralRe: collections doubt Pin
rwestgraham1-May-05 10:19
rwestgraham1-May-05 10:19 
GeneralCannot copy… It is being used by another person Pin
KreativeKai22-Apr-05 3:50
professionalKreativeKai22-Apr-05 3:50 
GeneralRe: Cannot copy… It is being used by another person Pin
Dave Kreskowiak22-Apr-05 5:02
mveDave Kreskowiak22-Apr-05 5:02 
GeneralRe: Cannot copy… It is being used by another person Pin
KreativeKai22-Apr-05 6:22
professionalKreativeKai22-Apr-05 6:22 
GeneralRe: Cannot copy… It is being used by another person Pin
Dave Kreskowiak22-Apr-05 7:05
mveDave Kreskowiak22-Apr-05 7:05 
GeneralRe: Cannot copy… It is being used by another person Pin
Dave Kreskowiak22-Apr-05 7:14
mveDave Kreskowiak22-Apr-05 7:14 
GeneralRe: Cannot copy… It is being used by another person Pin
rwestgraham25-Apr-05 9:01
rwestgraham25-Apr-05 9:01 
Generalweb services Pin
muktajoshi_1022-Apr-05 1:40
muktajoshi_1022-Apr-05 1:40 
GeneralRe: web services Pin
Dave Kreskowiak22-Apr-05 4:55
mveDave Kreskowiak22-Apr-05 4:55 
GeneralError Handling Pin
dpagka21-Apr-05 23:38
dpagka21-Apr-05 23:38 
GeneralRe: Error Handling Pin
Colin Angus Mackay22-Apr-05 0:01
Colin Angus Mackay22-Apr-05 0:01 
GeneralRe: Error Handling Pin
Dave Kreskowiak22-Apr-05 1:05
mveDave Kreskowiak22-Apr-05 1:05 
GeneralRe: Error Handling Pin
toxcct22-Apr-05 1:35
toxcct22-Apr-05 1:35 
GeneralRe: Error Handling Pin
Dave Kreskowiak22-Apr-05 4:51
mveDave Kreskowiak22-Apr-05 4:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.