about
12/09/2022
CsConcurrentFileAccess Repo
CsConcurrentFileAccess code

CsConcurrentFileAccess  Repository

Supports multiple concurrent file readers and writers

Quick Status Code functions correctly no known defects Demonstration code yes Documentation yes Test cases no Static library no Build requires C# 7.0 Planned design changes - Refactory into library
- Add more demonstrations

1.0 Concept

When a thread has opened a file for reading or writing, the .Net System.IO.File class will throw an exception if another file attempts to open for either reading1 or writing. This small library supports concurrent access by catching thrown exceptions, sleeping for a small interval and retrying the file open operation. It will do that a finite number of times before declaring failure2.

2.0 Design

The library provides two classes:
  • FileReader with methods:
    • bool Open(string filename)
    • void Close()
    • int ReadFile(string filename) - uses Open and Close
    • byte[] LastFileRead() - returns all bytes in filename
    and properties:
    • int NumAttempts - number of attempts to Open
    • int NumFailures - number of failures to Open
    • int SleepMilliSecs - time to wait in milliseconds between attempts to Open
  • FileWriter with methods:
    • bool Open(string filename)
    • void Close()
    • int CopyFile(string readPath, string writePath)
    and properties:
    • int NumAttempts - number of attempts to Open
    • int NumFailures - number of failures to Open
    • int SleepMilliSecs - time to wait in milliseconds between attempts to Open

3.0 Build

All code was built with Visual Studio, Community Edition - 2019, and tested on Windows 10.
  1. The System.IO.File class Open method supports FileShare.Read to allow multiple concurrent readers access to a shared file. That was used in this library to minimize exception handling for read operations.
  2. At this time, the library does not query the exception to see if continuing is feasible. For example, if the error is file access denial, no amount of retrying will succeed. Eventually the library will incorporate this query test.
  Next Prev Pages Sections About Keys