Instead of using List<T> , you should use ConcurrentBag<T> instead.
ConcurrentBag is a realistic package implementation optimized for scenarios where the same stream will produce and consume data stored in the bag.
Additional Information:
http://msdn.microsoft.com/en-us/library/dd381779.aspx
Also, be careful to create the number of threads to access, more than 100 threads will do slow performance, because the switching context takes time.
Edit: for .NET 3.5 you can make it thread safe using a simple lock
internal class EventManager { private static List<EventRecord> s_listEvents = new List<EventRecord>(); private static object _syncObject = new object(); public static void AddEvent(EventRecord record) { record.EventDate = DateTime.Now; lock(_syncObject) { s_listEvents.Add(record); } } public static List<EventRecord> GetRecordsByDate(DateTime date) { lock (_syncObject) { var r = (from l in s_listEvents where l.EventDate >= date select l).ToList<EventRecord>(); return r; } } }
Edit :
Depending on the situation, if you read the data very often, use ReaderWriterLockSlim with ReaderWriterLock best for the entire application, because it allows multiple threads to read data.
If not, use a lock that has better overall performance.
See link:
http://blogs.msdn.com/b/pedram/archive/2007/10/07/a-performance-comparison-of-readerwriterlockslim-with-readerwriterlock.aspx
source share