文件共享.None 是否使线程等待文件流关闭



当使用文件流并将FileShare设置为None时,并表示同时访问同一功能的两个用户想要读取/写入该文件。FileShare.None是让第二个用户的请求等待,还是第二个用户的请求会引发异常?

//two users get to this this code at the same time
using (FileStream filestream = new FileStream(chosenFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
using (StreamReader sr = new StreamReader(filestream))
using (StreamWriter sw = new StreamWriter(filestream))
{
    //reading and writing to file
}

Msdn 说: 无 拒绝共享当前文件。任何打开文件的请求(通过此进程或其他进程)都将失败,直到文件关闭。

但是请求会继续尝试直到文件流关闭吗?

当一个进程对一个文件进行读/写时FileShare.None任何进程对同一文件的任何后续访问都将导致Acess Denied Exception。要回答您的问题,第二个用户将获得异常。

MSDN:文件共享.无 - 拒绝共享当前文件。任何打开 文件(通过此进程或其他进程)将失败,直到文件被 闭。


有许多方法可以处理此类并发文件访问问题,以下代码演示了解决此问题的简单方法。

//Retry 5 times when file access fails
int retryCounter = 5;
while (!isFileAccessSuccess && retryCounter > 0)
{
    try
    {
       //Put file access logic here
        //If the file has been accessed successfully set the flag to true
        isFileAccessSuccess = true;
    }
    catch (Exception exception)
    {
        //Log exception
    }
    finally
    {
        //Decrease the retry count
        --retryCounter;
    }
    if (!isFileAccessSuccess)
    {
        //Wait sometime until initiating next try
        Thread.Sleep(10000);
    }
}

不,IOException会被抛出一个带有HResult = -2147024864Message = The process cannot access the file 'path' because it is being used by another process.

如果要同步对文件的访问,可以使用命名的等待句柄。

public class FileAcessSynchronizer
{
    private readonly string _path;
    private readonly EventWaitHandle _waitHandle;
    public FileAcessSynch(string path)
    {
        _path = path;
        _waitHandle =  new EventWaitHandle(true, EventResetMode.AutoReset, "NameOfTheWaitHandle");
    }
    public void DoSomething()
    {
        try
        {
            _waitHandle.WaitOne();
            using (FileStream filestream = new FileStream(chosenFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
            using (StreamReader sr = new StreamReader(filestream))
            using (StreamWriter sw = new StreamWriter(filestream))
            {
                //reading and writing to file
            }
        }
        finally
        {
            _waitHandle.Set();
        }
    }
}

由于命名的等待句柄会创建一个关键部分,因此应用程序的任何两个线程或进程(使用与等待句柄名称相同的名称)都不能同时执行其中的代码。因此,一个线程或进程进入该部分,以没有人可以访问它的方式打开文件(其他应用程序),执行命令,最后离开关键部分以允许应用程序的其他线程或进程进入关键部分。

最新更新