0

I have a multi thread application. My problem is that sometimes I get an error like the program cannot access to a file as it is been used by something else (other part of the application).

Right now what I did is catch the exception and repeat the task after a 100ms delay.

It is working as the application does not open the files so often and it is open the files just for a short period.

Though I am not happy with that solution as it is ugly, and it still can go wrong.

Are there any good ways to sort out that problem? If there are not any easy answer and you need more information about the application(what files, why I might access in the same time) I can answer, I just try to keep the question simple, and generic.

Regards Daniel

EDIT:

I made up something fast, what do you guys think about that solution, it is not the most generic one, but had no better idea just now:

When the program try to write into the file check if the it is used or not. If it is used collect a data in the memory, as soon as the application want to write in to the file again and the file is free insert in all of the data.

It still not the best, but it might give some more ideas.

4

1 回答 1

3

由于您的应用程序既要读取文件又要写入文件,因此您需要做的就是跟踪您的应用程序正在使用哪些文件。保留按文件名键入的R/W 锁字典。当你去访问一个文件时,取出相应的锁。

以下是您可能的编码方式:

Dictionary<string, ReaderWriterLockSlim> lockDict = new Dictionary<string, ReaderWriterLockSlim>();

ReaderWriterLockSlim LockFile(string filename, bool readOnly)
{
    var fullPath = System.IO.Path.GetFullPath(filename);
    ReaderWriterLockSlim myLock;
    // lock the dictionary while we're accessing it
    lock (lockDict)
    {
        if (!lockDict.TryGetValue(fullPath, out myLock))
        {
            myLock = new ReaderWriterLockSlim();
            lockDict[fullPath] = myLock;
        }
    }
    // only block on the file lock once the dictionary is unlocked
    if (readOnly)
        myLock.EnterReadLock();
    else
        myLock.EnterWriteLock();
    // file is now "locked", so caller can proceed with read/write, then unlock
    return myLock;
}

你可能会像这样使用它:

// block until we're not using the file anymore
var myLock = LockFile(filename, readOnly: false);
try
{
    using (var file = new StreamWriter(filename))
    {
    ...
    }
}
finally
{
    myLock.ExitWriteLock();
}
于 2013-05-28T16:23:52.240 回答