The SyncLock Statement

  • Thread starter Thread starter Ram
  • Start date Start date
R

Ram

Hey,
I have about 5 Shared variables, that I want to be locked for access when
they are written to.
I'v read in the MSDN about the SyncLock statement, and I'm prety sure that's
what I'm looking for,
But the SycLock statement requires a statement in it's decleration:
SyncLock <Statement>
MySharedMember=NewValue
End SyncLock
What the statement's supposed to be?
Thanks ahead

-- Ram
 
Ram said:
I have about 5 Shared variables, that I want to be locked
for access when they are written to.

This is the C# newsgroup. VB is dealt with in
microsoft.public.dotnet.languages.vb.
But the SycLock statement requires a statement in it's
decleration:
SyncLock <Statement>
MySharedMember=NewValue
End SyncLock
What the statement's supposed to be?

That code snippet is probably an assignment, not a declaration. You
generally need to lock a variable when you update it (to prevent other
code updating it at the same time), not when you first declare it.

P.
 
The SyncLock (or lock, as it's called in C#) takes an expression as a
parameter that evaluates to an instance variable. The instance is used as a
mutex and is locked throughout the scope of the SyncLock/lock statement.
This means that anything else that tries to run a SyncLock or lock on that
same mutex (instance) will block until the currently locking code is
complete.

Basically, declare an instance -- any instance. A new object() if you want.
Then use that in any calls to SyncLock that you wish to be mutually
exclusive, ie. you wish to only have one of those bits of code run at any
one time.
 
John Wood said:
The SyncLock (or lock, as it's called in C#) takes an expression as a
parameter that evaluates to an instance variable.

No, it takes *any* expression which is a reference type. It may happen
to be an instance variable, but it certainly doesn't have to be.
 
I actually meant to write 'instance reference'... coz an expression can't
evaluate to a variable! Doh.
 
Back
Top