0

How to make dictionary thread safe during async tasks that adds and read from the dictionary?

I got a case where I got like 10 async tasks running on their own threads, which retrieves files from a ftp server. What each process do with the dictionary is that it add the filename and last written size to the dictionary, and since all the process is reading and writing to the dictionary at the same time, I'm a bit afraid of exceptions such as Enumerable has been modified, which I got during working with List and async tasks processes.

How can I solve this problem?

1 Answers1

3

I think you may be looking for ConcurrentDictionary<TKey,TValue>?

Has thread-safe operations and adds a few new methods like TryAdd and AddOrUpdate that would be helpful in a concurrent environment

Simon Reynolds
  • 329
  • 3
  • 7
  • Should the cache only be in that dictionary or should I use it in the methods that also generate the dictionary to be processed to cache? – ghost media Jun 20 '19 at 08:09