检查文件是否被其他可执行运行实例使用 [英] Check If File Is In Use By Other Instances of Executable Run

查看:104
本文介绍了检查文件是否被其他可执行运行实例使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我详细介绍之前,我的程序是使用C#.Net 4.0在Visual Studio 2010中编写的。

Before I go into too detail, my program is written in Visual Studio 2010 using C# .Net 4.0.

我编写了一个程序,它将生成单独的日志文件每次运行。日志文件以时间命名,精确到毫秒(例如, 20130726103042375.log )。如果该日程尚不存在,该程序还将生成当天的主日志文件(例如,* 20130726_Master.log *)

I wrote a program that will generate separate log files for each run. The log file is named after the time, and accurate up at millisecond (for example, 20130726103042375.log). The program will also generate a master log file for the day if it has not already exist (for example, *20130726_Master.log*)

在每次运行结束时,我想将日志文件附加到主日志文件。有没有办法检查我是否可以成功追加?并在 Sleep 之后重试一段时间等等?

At the end of each run, I want to append the log file to a master log file. Is there a way to check if I can append successfully? And retry after Sleep for like a second or something?

基本上,我有1个可执行文件和多个用户(假设有5个用户。)

Basically, I have 1 executable, and multiple users (let's say there are 5 users).

所有5个用户将同时访问并运行此可执行文件。由于所有用户几乎不可能在完全相同的时间(最长毫秒)启动,因此生成单个日志文件没有问题。

All 5 users will access and run this executable at the same time. Since it's nearly impossible for all user to start at the exact same time (up to millisecond), there will be no problem generate individual log files.

然而,问题来了在我尝试将这些日志文件合并到主日志文件时。虽然不太可能,但我认为如果多个用户附加到同一主日志文件,程序将崩溃。

However, the issue comes in when I attempt to merge those log files to the master log file. Though it is unlikely, I think the program will crash if multiple users are appending to the same master log file.

我使用的方法是

File.AppendAllText(masterLogFile, File.ReadAllText(individualLogFile));

我已经检查了 lock 对象,但我认为它在我的情况下不起作用,因为在一个实例中有多个实例而不是多个线程。

I have check into the lock object, but I think it doesn't work in my case, as there are multiple instances running instead of multiple threads in one instance.

我调查的另一种方式是 try / catch ,类似这样

Another way I look into is try/catch, something like this

try
{
    stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch {}

但我不认为这解决了问题,因为masterLogFile的状态可以在那个短暂的毫秒内改变。

But I don't think this solve the problem, because the status of the masterLogFile can change in that brief millisecond.

所以我的整体问题是:有没有办法追加到masterLogFile如果没有使用,如果是短暂超时后重试?或者,如果有另一种方法来创建masterLogFile?

So my overall question is: Is there a way to append to masterLogFile if it's not in use, and retry after a short timeout if it is? Or if there is an alternative way to create the masterLogFile?

提前谢谢你,对不起这条长信息感到抱歉。我想确保收到我的信息并解释我尝试或调查的内容,这样我们就不会浪费任何人的时间。

Thank you in advance, and sorry for the long message. I want to make sure I get my message across and explain what I've tried or look into so we are not wasting anyone's time.

如果有的话请告诉我我可以提供的信息可以帮助你。

Please let me know if there's anymore information I can provide to help you help me.

推荐答案

你的尝试/捕获是做事的方法。如果对 File.Open 的调用成功,则可以写入该文件。我们的想法是保持文件打开。我会建议像:

Your try/catch is the way to do things. If the call to File.Open succeeds, then you can write to to the file. The idea is to keep the file open. I would suggest something like:

bool openSuccessful = false;
while (!openSuccessful)
{
    try
    {
        using (var writer = new StreamWriter(masterlog, true)) // append
        {
            // successfully opened file
            openSuccessful = true;
            try
            {
                foreach (var line in File.ReadLines(individualLogFile))
                {
                    writer.WriteLine(line);
                }
            }
            catch (exceptions that occur while writing)
            {
                // something unexpected happened.
                // handle the error and exit the loop.
                break;
            }
        }
    }
    catch (exceptions that occur when trying to open the file)
    {
        // couldn't open the file.
        // If the exception is because it's opened in another process,
        // then delay and retry.
        // Otherwise exit.
        Sleep(1000);
    }
}
if (!openSuccessful)
{
    // notify of error
}

因此,如果您无法打开文件,请进入睡眠状态并重试。

So if you fail to open the file, you sleep and try again.

查看我的博客发布, File.Exists只是一个快照,更多细节。

See my blog post, File.Exists is only a snapshot, for a little more detail.

这篇关于检查文件是否被其他可执行运行实例使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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