咨询区

  • Homam

我先说一下我的顾虑,是这样的,我有一个 windows service 服务在不断的写入日志,同时我还有个看板程序在不断的获取 windows service 程序写入的日志。

问题是:service 程序在写日志的时候会 lock 文件,看板程序在读取的时候也会 lock 文件,这就很尴尬了,经常遇到进程被占用的异常。

服务程序代码:


void WriteInLog(string logFilePath, data)
{File.AppendAllText(logFilePath, string.Format("{0} : {1}\r\n", DateTime.Now, data));
}

看板程序代码:

int index = 0;private void Form1_Load(object sender, EventArgs e){try{using (StreamReader sr = new StreamReader(logFilePath)){while (sr.Peek() >= 0)  // reading the old data{AddLineToGrid(sr.ReadLine());index++;}sr.Close();}timer1.Start();}catch (Exception ex){MessageBox.Show(ex.Message);}}private void timer1_Tick(object sender, EventArgs e){using (StreamReader sr = new StreamReader(logFilePath)){// skipping the old data, it has read in the Form1_Load event handlerfor (int i = 0; i < index ; i++) sr.ReadLine();while (sr.Peek() >= 0) // reading the live data if exists{string str = sr.ReadLine();if (str != null){AddLineToGrid(str);index++;}}sr.Close();}}

请问大家我该如何解决此问题?

回答区

  • Manfred

你的问题在于写入和读取都是以排他的方式进行,其实 FileStream 中是可以支持共享的方式进行读写的。

比如将 service 代码改造如下:


var outStream = new FileStream(logfileName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);

看板程序改造如下:


var inStream = new FileStream(logfileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

用完的时候记得 Close 一下,对了, FileStream 实现了 IDisposable 接口,可以用 using 包一下,参考如下代码:


using(var outStream = ...)
{// using outStream here...
}

点评区

????,我特意看了下 FileStreamFileShare.ReadWrite 参数解释。

    [ComVisible(true)][Flags]public enum FileShare{None = 0,Read = 1,Write = 2,//// Summary://     Allows subsequent opening of the file for reading or writing. If this flag is//     not specified, any request to open the file for reading or writing (by this process//     or another process) will fail until the file is closed. However, even if this//     flag is specified, additional permissions might still be needed to access the//     file.ReadWrite = 3,Delete = 4,Inheritable = 16}

我去,原来还可以这样玩,对 FileStream 用的还不透彻,学习了。

NET问答: 如何实现读写 file 的时候不用锁模式 ?相关推荐

  1. 表级锁的mysql读写_Mysql的表级锁

    我们首先需要知道的一个大前提是:mysql的锁是由具体的存储引擎实现的.所以像Mysql的默认引擎MyISAM和第三方插件引擎 InnoDB的锁实现机制是有区别的.可根据不同的场景选用不同的锁定机制. ...

  2. gcore 获取程序core dump file 但程序不用退出,gdb 分析core

    转载地址: http://hi.baidu.com/widebright/item/e94ea43535711f4a3075a1d0 本文包含如下部分内容 1. Linux内核里面生成的core fi ...

  3. MODBUS工业自动化RFID高频读写器读写头CK-FR03-A01的其他模式配置命令说明

    工业自动化RFID高频读写器读写头CK-FR03-A01是一款基于射频识别技术的高频RFID标签读卡器,读卡器工作频率为13.56MHZ,支持对I-CODE 2.I-CODE SLI等符合ISO156 ...

  4. SD卡读卡程序 C语言 IO模拟,请问大神些,有谁用过SD模式读写SD卡?时序是用单片机IO模拟。...

    大家读写SD卡怎么不用SD模式方式读取,是SPI方式读取简单? 下面贴一段代码,是在FPGA上程序,只能用SD模式进行读,没有文件系统,没有用AVR硬件实现, 电路图: image001.jpg (2 ...

  5. C/C++文件读写操作——FILE*、fstream

    基于C的文件操作 在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O文件操作,下面就分别介绍之. 一.流式文件操作 这种方式的文件操作有一个重要的结构FILE,FILE在头文件stdi ...

  6. C/C++文件读写操作总结:FILE*、fstream、windowsAPI

    CFile file;CString FileName = _T("C:\\Users\\zhan\\Desktop\\自写测试数据\\100.txt");file.Open(Fi ...

  7. C/C++文件读写操作——FILE*、fstream、windowsAPI

    转载自: http://blog.sina.com.cn/s/blog_6e7fed390100z0j1.html 基于C的文件操作 在ANSI C中,对文件的操作分为两种方式,即流式文件操作和I/O ...

  8. python json数据的文件读写操作

    python json数据的文件操作 代码 read_write_json.py #!/usr/bin/env python # -*- encoding: utf-8 -*- "" ...

  9. idea 文件流读取web-inf下的文件_C#初学者教程系列20:Stream流读写

    本文是C#初学者简单教程,这是第20篇.感谢观看,记得关注我,后续还有更多教程文章,谢谢. 本文环境为Visual Studio 2019. 一.介绍 在C#文件操作中,通常使用流来读取和写入文件.流 ...

最新文章

  1. RabbitMQ是什么
  2. foo php,php – $foo === TRUE和TRUE === $foo有什么区别?
  3. python计算召回率_序列标注的准确率和召回率计算
  4. 第四范式AI新品发布会报名开启(8月20日·上海)
  5. 测试TensorFlow Object Detection API
  6. android 编程一个程序实现方法,Android中一个应用实现多个图标的几种方式
  7. 让低版本浏览器支持html5的标签
  8. Oracle日志切换及频率跟踪脚本
  9. envi图像裁剪_【ENVI基础】如何进行水体提取?
  10. win8 计算机服务,Win8.1系统下哪些服务可以关闭
  11. HTTP 错误 404.3 - Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
  12. 计算机论文摘要200字模板,设计论文摘要万能模板_论文摘要万能模板_论文摘要200字模板...
  13. Sphinx/coreseek/mysql全文检索
  14. 2017中国之旅系列之九:山西平遥古城之旅
  15. Android 视频直播的流程总览
  16. linux下Umail服务的停止
  17. requires that an attribute name is preceded by whitespace异常
  18. 论文笔记:NAOMI: Non-Autoregressive MultiresolutionSequence Imputation
  19. Nginx安装/负载均衡/反向代理配置与调优
  20. 数据分析-思维分析逻辑day05

热门文章

  1. AddTransient、AddSingleton、AddScoped 三者都应该在什么场景下使用
  2. 【概率论】1-2:计数方法(Counting Methods)
  3. WPF 绘制对齐像素的清晰显示的线条
  4. easy html5 - Jquery mobile
  5. 优秀编程网站收录集锦
  6. 如何提高gps精度_如何在锻炼应用程序中提高GPS跟踪精度
  7. 从Boxee的Amie Street访问音乐
  8. 项目中的模块剥离成项目_使用MCEBuddy 2从电视录制中剥离广告
  9. dock怎么自定义_如何自定义和调整Mac的Dock
  10. Django中session和cookie简单的使用