So the first key point is that you can use a FileSystemWatcher
to be notified when a file system event changes at a particular path. If you, for example, want to be notified when a file is created at a particular location you can find out.
Next, we can create a method that uses a TaskCompletionSource
to trigger the completion of a task when the file system watcher triggers the relevant event.
public static Task WhenFileCreated(string path)
{
if (File.Exists(path))
return Task.FromResult(true);
var tcs = new TaskCompletionSource<bool>();
FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(path));
FileSystemEventHandler createdHandler = null;
RenamedEventHandler renamedHandler = null;
createdHandler = (s, e) =>
{
if (e.Name == Path.GetFileName(path))
{
tcs.TrySetResult(true);
watcher.Created -= createdHandler;
watcher.Dispose();
}
};
renamedHandler = (s, e) =>
{
if (e.Name == Path.GetFileName(path))
{
tcs.TrySetResult(true);
watcher.Renamed -= renamedHandler;
watcher.Dispose();
}
};
watcher.Created += createdHandler;
watcher.Renamed += renamedHandler;
watcher.EnableRaisingEvents = true;
return tcs.Task;
}
Note that this first checks if the file exists, to allow it to exit right away if applicable. It also uses both the created and renamed handlers as either option could allow the file to exist at some point in the future. The FileSystemWatcher
also only watches directories, so it's important to get the directory of the specified path and then check the filename of each affected file in the event handler.
Also note that the code removes the event handlers when it's done.
This allows us to write:
public static async Task Foo()
{
await WhenFileCreated(@"C:Tempest.txt");
Console.WriteLine("It's aliiiiiive!!!");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…