Oh, this is a pretty basic, but shows some musunderstandings.

Inline... 

> -----Original Message-----
> From: Unmoderated discussion of advanced .NET topics. 
> [mailto:[EMAIL PROTECTED] On Behalf Of 
> [EMAIL PROTECTED]
> Sent: Dienstag, 12. Oktober 2004 10:08
> To: [EMAIL PROTECTED]
> Subject: [ADVANCED-DOTNET] Multithreading question
> 
> I am having trouble understanding .net and c#'s lock command
> 
> What happens if a lock is called on a object that is already 
> being used by another process.

Can not happen. See, another process = another appdomain, and you can
not hold locks cross process.

> I am getting weird behavior in dotnet remoting to do with threads.

Carefull here - what remoting does with threads is also part of the
problem. DSOmetimes. Your problem is different, though.

> If i put a lock on all methods in there, i get a deadlock 
> which i would expect, but when i only put a lock on the only 
> method that enumerates through a arraylist i get a 
> enumeration violation because of modification to the array 
> during enumeration.  I dont see how this is possible if i put 
> a lock on the object that contains the arraylist while i 
> enumerate through it. ?!?!?!?!

You do NOT put a lock ON an object. The locking statement does NOT mean
that the OBJECT is locked. It means that any other attempt to use th
eobject as a lock will fail.

Basically, every lock needs something like a unique reference - so that
you can have multiple sepratate locks, and the .net runtime allows you
to use ANY object as a reference. This does NOT create a lock ON the
object.

> I am thinking if another thread already is already in that 
> object both may execute at the same time regardsless of the 
> lock block.

No. Basically a lock means that any other thread trying to GET the same
lock will have to wait. It does not mean that a thread not checking for
the lock (!) will be prohibited at all to operate.

SO, oif through remoting another thread comes in and modifies the object
- not asking for a lock - it will modify the object, and boom.

Which means, in short, that you should not expose the object through
remoting, but a wrapper that makes sure that the necessary locks are
obeyed.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
(CTO PowerNodes Ltd.)

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to