FileSystemWatcher を使用して、システムのフォルダーにファイルが到着するたびに通知します。
時々、それらは (他のプログラムによって) ロックされた状態で到着します。
私は以下のようなことをしたい:
タイムアウト後もロックされている場合はそのファイルを無視するか、タイムアウト中にロックが解除された場合はファイルを処理します
私は現在、この解決策を思いつきましたが、それを達成する他の方法があるかどうか疑問に思っています。
lockedFilePaths = new List<string>();
NoLockFilePaths = new List<string>();
Watcher.Created += (sender, e) => WatcherHandler(e.FullPath);
WatcherHandler(string FilePath)
{
CheckFileAccess(FilePath);
if (NoLockFilePaths.Any())
{
//Process all file paths
}
}
CheckFileAccess(string filepath)
{
// start a timer and invoke every say 10ms
// log the locked time of the file.
// compare the current time.
// return null if TIMEOUT exceeds
// or wait till the TIMEOUT and keep on checking the file access
}
問題は、CheckFileAccess をシンプルかつ最適に実装する方法です。
現在、System.Threading.Timer を使用して 1000 ミリ秒ごとに通知し、ファイルがまだロックされているかどうかを確認し、実装が満足できるものではないかどうかを確認します。いくつかの非常に単純な実装の提案を探しています。