多线程文件访问(锁定控制) [英] Multi-thread file access (locking control)

查看:102
本文介绍了多线程文件访问(锁定控制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个程序在同一个文件
上工作,我希望第一个程序定期检查该文件,如果对文件进行了任何更改,则将其清空。另一个程序将
写入文件。
问题发生在第二个程序尝试在文件上写时,因为它是从第一个程序开始使用的,
是否有(如果有任何实现的C#程序会更好)算法来处理? p>

解决方案

要检查文件是否已更改,您可能首先要检查其时间戳。在伪代码中,您可以在监视程序中尝试以下操作:

  try {
if(FileTimeStampHasChanged(FileName )){
//尝试对文件
进行操作}
}
catch {
//处理或忽略异常(如果无法打开文件文件(也许其他应用程序尚未完成写入)。您以后可以随时重试。
}

但是,我强烈建议完全采用其他方法。以下是我建议的选项。 > output。$$$ ),并在完成编写后立即对其进行重命名。重命名是瞬时的,不会引起任何锁定。根据我的经验,这是并发访问的最佳简单解决方案之一。

  • 如果无法更改书写程序,请修改阅读程序。使用我在第1点中描述的逻辑,让它尝试重命名文件以读取并处理重命名的副本。如果重命名失败,则表示编写者未完成,以后可以尝试。

  • 如果您无法更改任何程序,则可以制作自己的程序。实现第1点的交通管理员应用程序。它需要做更多的工作,但仍然可行。操作方法如下:

    • 配置Writer以写入文件夹,例如

    • 配置阅读器以从另一个文件夹读取,例如输入。

    • 编写交通督导员,以便监视输出文件夹,并在文件更改时将其移至输入文件夹。移动也是瞬时的(与重命名实际上是相同的操作)。

  • $ li $ b


    i have two programs working on the same file i want the first program to check regularly on the file, and if any changes are made operate on the file and empty it. the other program writes to the file. the problem occurs when the second program tries to write on the file because it's used from the first one is there an (if there's any implemented C# program would be even better) algorithm to handle this?

    解决方案

    To check if file has changed you might first of all check its timestamp. In pseudo code, you might try something like this in the monitoring program:

    try {
      if(FileTimeStampHasChanged(FileName)) {
        //Attempt to do something with the file
      }
    }
    catch {
      // Handle or ignore the exception if you can't open the file (maybe the other application hasn't finished writing). You can always retry later.
    }
    

    However, I would strongly recommend a different approach altogether. Here are the options I'd suggest.

    1. If you can alter the writing program, let it write to a temporary file (e.g. output.$$$) and let it rename it as soon as it's done writing. Renaming is instantaneous and doesn't cause any locking. This is, in my experience, one of the best simple solutions for concurrent access.
    2. If you cannot alter the writing program, then modify the reading one. Using the logic I described in point 1, let it try to rename the file to read and work on the renamed copy. If the rename fails, then it means that the writer isn't done, and an attempt can be done later.
    3. If you cannot alter any of the programs, then you can make your own "traffic warden" application to implement point 1. It requires more work, but it's still doable. Here's how:
      • Configure the Writer to write into a folder, e.g. "Output".
      • Configure the Reader to read from another folder, e.g. "Input".
      • Write your Traffic Warden so that it monitors Output folder and, when the file changes, it moves it to Input folder. Moving is instantaneous as well (it's practically the same operation as renaming).

    这篇关于多线程文件访问(锁定控制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆