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 relevant as to what order it is being read in. The data is just being used to populate objects. My problem is even though I am opening the file each time in the thread as read only it complains that it is open by another program. (I don't have it opened in a text editor or anything :))
How can I accomplish multi reads from the same file?
EDIT: The file is ~18KB pretty small. It is read from about 1,800 times.
Thanks


If you want multiple threads to read from the same file, you need to specify FileShare.Read:

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

However, you will not achieve any speedup from this, for multiple reasons:

  • Your hard disk can only read one thing at a time. Although you have multiple threads running at the same time, these threads will all end up waiting for each other.
  • You cannot easily parse a part of an XML file. You will usually have to parse the entire XML file every time. Since you have multiple threads reading it all the time, it seems that you are not expecting the file to change. If that is the case, then why do you need to read it multiple times?

原文链接

Multiple Threads reading from the same file(转载)的更多相关文章

  1. Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类

    Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...

  2. caffe网络在多线程中无法使用GPU的解决方案 | cpp caffe net run in multiple threads

    本文首发于个人博客https://kezunlin.me/post/8d877e63/,欢迎阅读! cpp caffe net run in multiple threads Guide set_mo ...

  3. Apache POI – Reading and Writing Excel file in Java

    来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...

  4. [Training Video - 6] [File Reading] Making a Jar file with eclispe, Importing custom jars in SoapUI

    Code example : package com.file.properties; import java.io.FileInputStream; import java.util.Propert ...

  5. [Training Video - 6] [File Reading] [Java] Read Excel File Using Apache POI API

    读取以下两种格式的Excel : *.xls  and *.xlsx 用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库 HSSF is the POI Project's ...

  6. [Training Video - 6] [File Reading] [Java] Read Properties file

    package com.file.properties; import java.io.FileInputStream; import java.util.Properties; public cla ...

  7. 临界区代码 critical section Locks and critical sections in multiple threads

    临界区 在同步的程序设计中,临界区段(Critical section)指的是一个访问共享资源(例如:共享设备或是共享存储器)的程序片段,而这些共享资源有无法同时被多个线程访问的特性. 当有线程进入临 ...

  8. How to: Reading an ini config file from a batch file

    Original Link: http://almanachackers.com/blog/2009/12/31/reading-an-ini-config-file-from-a-batch-fil ...

  9. SQLite multiple threads

    const int loops = 1000; public void DatabaseThreadSafetyTest() { var backgroundThread = new Thread(n ...

随机推荐

  1. vue2.0 element-ui中的el-select选择器无法显示选中的内容

    我使用的是element-ui V2.2.3.代码如下,当我选择值得时候,el-select选择器无法显示选中的内容,但是能触发change方法,并且能输出选择的值. select.vue文件 < ...

  2. 微信小程序顶部(navigationBar)设置为透明

    我记得在微信小程序中导航栏的颜色可以在app.json.  window里面添加navigationBarBackgroundColor属性,但是颜色只能为纯色.不能使用rgb,或者rgba的色号. ...

  3. vue-cli脚手架之package.json

    package.json文件配置及其含义,这个是vue-cli自动生成的文件,先贴一张代码及其含义: { "name": "secondproject",//模 ...

  4. OSPF协议总结

    总结: 1.ospf协议报文不会泛洪扩散,而是逐级路由器处理后,再从所有ospf启用端口发送出去,也就是说,只能从邻居接收到ospf报文,报文的源ip是邻居的ip地址,目的ip是组播ip. 2.开启o ...

  5. Linux常用系统命令

    致歉:各位看到此博客的朋友们 因为命令的数量挺多的很多命令也都很简单  我就总结了一下具体的命令和这个命令是做什么的,主要的使用方法是链接到http://man.linuxde.net/的网站的,请各 ...

  6. Windows中几个内存相当的指标

    以下几个内存大小相当: IS:虚拟内存任务管理器:提交内存进程对象上的:PrivateMemorySize64,性能计数器:Process\Private Bytes

  7. SoapUI SoapUI测试WebService协议接口简介

    SoapUI测试WebService协议接口简介 by:授客 QQ:1033553122 1. 创建项目,入口:File -> New SOAP Project,或者右键默认项目Project- ...

  8. Testlink Testlink在Windows下的安装

    Testlink在Windows下的安装   by:授客 QQ:1033553122   测试环境 testlink-1.9.14 下载地址:http://pan.baidu.com/s/1pLrcu ...

  9. loadrunner 脚本开发-int型变量和字符串的相互转换

    脚本开发-int型变量和字符串的相互转换 by:授客 QQ:1033553122 字符串转化为int型变量 Action2() { int j = 0; j = atoi("12345&qu ...

  10. python与MongoDB的基本交互:pymongo

    本文内容: pymongo的使用: 安装模块 导入模块 连接mongod 获取\切换数据库 选择集合 CRUD操作 首发时间:2018-03-18 20:11 pymongo的使用: 安装模块: pi ...