[使用C#和Windows作为平台]
我有一台相机,可以将 JPG 文件写入电脑的本地文件夹中。我想加载相机丢弃的每个文件,因此我有一个 FileSystemWatcher,它会在创建新图片时通知我,但相机在写入文件时锁定文件,所以如果我在收到创建通知后尝试加载它,我会收到一个异常:文件被锁定.
目前,我有一个 while 循环(带有 Thread.Sleep),每 0.2 秒重试一次加载图像,但感觉有点脏。
有没有更优雅的方法来等待锁被释放,这样我就可以加载文件并确保它不再使用?
您将无法绕过试错方法,即尝试打开文件,捕获 IOException,然后重试。但是,您可以将这种丑陋隐藏在单独的类中,如下所示:
public class CustomWatcher
{
private readonly FileSystemWatcher watcher;
public event EventHandler<FileSystemEventArgs> CreatedAndReleased;
public CustomWatcher(string path)
{
watcher = new FileSystemWatcher(path, "*.jpg");
watcher.Created += OnFileCreated;
watcher.EnableRaisingEvents = true;
}
private void OnFileCreated(object sender, FileSystemEventArgs e)
{
// Running the loop on another thread. That means the event
// callback will be on the new thread. This can be omitted
// if it does not matter if you are blocking the current thread.
Task.Run(() =>
{
// Obviously some sort of timeout could be useful here.
// Test until you can open the file, then trigger the CreeatedAndReleased event.
while (!CanOpen(e.FullPath))
{
Thread.Sleep(200);
}
OnCreatedAndReleased(e);
});
}
private void OnCreatedAndReleased(FileSystemEventArgs e)
{
CreatedAndReleased?.Invoke(this, e);
}
private static bool CanOpen(string file)
{
FileStream stream = null;
try
{
stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
return false;
}
finally
{
stream?.Close();
}
return true;
}
}
这个“观察者”可以这样使用:
var watcher = new CustomWatcher("path");
watcher.CreatedAndReleased += (o,e) =>
{
// Now, your watcher has managed to open and close the file,
// so the camera is done with it. Obviously, any other application
// is able to lock it before this code manages to open the file.
var stream = File.OpenRead(e.FullPath);
}
免责声明:CustomWatcher 可能需要 IDisposable 并适当地处置 FileSystemWatcher。该代码仅显示如何实现所需功能的示例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)