How about.....
lock( myList.SyncRoot)
{
etc.....
}
On Tuesday, October 12, 2004, at 02:53PM, Steve Johnson <[EMAIL PROTECTED]> wrote:
>When you "lock" an object, there's no protection against other threads
>accessing that object. The system simply enforces the constraint that
>only 1 thread that "asks" for the lock will be granted that lock at a
>time. You need to ensure that every place in your code that accesses
>your array list asks for the same lock first. The object referenced
>in your lock statement doesn't even have to be the object you're
>trying to protect. As long as every place in the code that accesses
>the array list uses the same lock, you're fine.
>
>class myclass
>{
> private ArrayList myList = new ArrayList();
>
> void func1()
> {
> // will block if any other thread holds a lock on myList
> lock(myList)
> {
> foreach(Item myItem in myList)
> ...
> }
> }
>
> void func2()
> {
> // not protected. Even if another thread holds a lock, access is not
> // prevented here.
> myList.Add(anotherItem);
> }
>}
>
>
>To fix this sample, you'd wrap the myList.Add() statement with
>lock(myList). The synchronization system here provides protection
>against concurrency only when everybody plays by the same rules.
>
>
>--
>Steve Johnson
>
>On Tue, 12 Oct 2004 21:07:46 +1300, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>> 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.
>>
>> I am getting weird behavior in dotnet remoting to do with threads.
>>
>> 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. ?!?!?!?!
>>
>> I am thinking if another thread already is already in that object both
>> may execute at the same time regardsless of the lock block.
>>
>> Can anyone confirm or deny my theory and give me some advice on what
>> might be going on.
>
>===================================
>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
>
>
===================================
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