FilePathGetFileLockT Method

Gets a lock on the file using the given lock function.

Definition

Namespace: Gemstone.IO
Assembly: Gemstone.Common (in Gemstone.Common.dll) Version: 1.0.128 -- Release Build+d050cfc5563c89a1188cc3c6b2d417530856f490
public static T GetFileLock<T>(
	string fileName,
	Func<string, T> lockFunction,
	double secondsToWait = 5,
	int retryMilliseconds = 200
)

Parameters

fileName  String
The name of the on which the lock is to be obtained.
lockFunction  FuncString, T
The function to be called in order to get the file lock.
secondsToWait  Double  (Optional)
The number of seconds to wait before giving up on the file lock.
retryMilliseconds  Int32  (Optional)
The number of milliseconds to wait between attempts to obtain the file lock.

Type Parameters

T
The return value of the lock function.

Return Value

T
The return value of the lock function.

Remarks

The intent of this function is to provide a sane method for opening a file which may produce errors due to read/write contention. Usage of this class is fairly simple using the static methods built into the File class.

C#
using (StreamReader reader = GetFileLock(File.OpenText))
{
    // Read lines from the file
}
C#
using (FileStream stream = GetFileLock(File.Create))
{
    // Write bytes into the file
}

This method will only retry if an IOException occurs while executing the lockFunction. After retrying for at least secondsToWait seconds, this function will throw the last IOException it encountered.

See Also