0

There are several places in my code (C#) which will modify a queue or a list. I want to make sure at any time there is only one thread modify the queue or list. so How to keep its consistency in C#? thanks

Edit Please see sample code COMException on Main Thread of WPF application

Community
  • 1
  • 1
toosensitive
  • 2,335
  • 7
  • 46
  • 88

5 Answers5

2

Hold a mutex while accessing or modifying the queue or list.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

Use the lock statement.

    lock(myObject) {

    //Code

    }
Dot NET
  • 4,891
  • 13
  • 55
  • 98
2

There are several threadsafe types available in the System.Collections.Concurrent namespace and elsewhere in the framework. Using these may be the simplest option.

You could also perform your own explicit locking using the lock statement. E.g.

public class Foo
{
    private readonly List<int> list = new List<int>();
    private readonly object locker = new object();

    public void SomeCommand(Bar bar)
    {
        lock (this.locker)
        {
            // do something here?
            this.list.Add(..);
            // do something here?
        }
    }

    public Baz SomeQuery()
    {
        lock (this.locker)
        {
            // do something here?
            this.list.Select(...);
            // do something here?
        }
    }

    public void SomethingElseEntirely()
    {
        lock (this.locker)
        {
            // do something here
        }            
    }
}

The best option varies from case to case. E.g. do you want to also synchronize operations other than just the collection manipulations (as indicated in the code sample), do you want to performance tune for more reads than writes or more writes than reads (some of the built in types are already tuned in these ways), etc.

You could also experiment with other explicit thread synchronization types, such as ReaderWriterLockSlim which is optimised for more reads than writes, and see what fits your scenario best.

Adam Ralph
  • 29,453
  • 4
  • 60
  • 67
  • I'd like to synchronize colleciton manipulations and also execution of several code sections (where the collection is accessed), i.e. at any time only one section is run – toosensitive Jan 09 '12 at 21:22
  • A lock will work as advertised. What problem are you experiencing? – Adam Ralph Jan 09 '12 at 22:17
  • I use lock in every section that need running exclusively and I still get COMException Message:Exception from HRESULT: 0x800AC472, pls see sample code here http://stackoverflow.com/questions/8566214/comexception-on-main-thread-of-wpf-application – toosensitive Jan 10 '12 at 22:59
  • I believe the COM exception is a separate problem from this question. This question is about synchronising thread access to lists and IMHO this has been answered. – Adam Ralph Jan 11 '12 at 07:20
2

If you are on .NET 3 or later, the simplest solution is to use SynchronizedCollection.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

It's best to use built-in thread-safe collections such as ConcurrentQueue.

Tudor
  • 61,523
  • 12
  • 102
  • 142