Click or drag to resize

FilePathGetFileLockT Method

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

Namespace: GSF.IO
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.218-beta+101eee949414e414795e55a6e73d88938f0177b8
Syntax
public static T GetFileLock<T>(
	string fileName,
	Func<string, T> lockFunction,
	double secondsToWait = 5,
	int retryMilliseconds = 200
)
View Source

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