Multiple Threads reading from the same file

前端 未结 3 1323
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 19:38

I have a xml file that needs to be read from many many times. I am trying to use the Parallel.ForEach to speed this processes up since none of that data being read in is re

3条回答
  •  佛祖请我去吃肉
    2020-12-14 20:09

    When you open the file, you need to specify FileShare.Read :

    using (var stream = new FileStream("theFile.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        ...
    }
    

    That way the file can be opened multiple times for reading

提交回复
热议问题