PollingFilterWatchToken
PollingFilterWatchToken(fileProvider, filterPath, pollInterval) is a single use polling file monitor. It detects file additions, deletions and modifications.
// File pattern to monitor
string filterPath = "**/*.txt";
// Create IFileProvider
IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
// Create initial token
IChangeToken token = new PollingFilterWatchToken(fileProvider, filterPath, TimeSpan.FromSeconds(0.5));
// Place here callback
Action<object?>[] callbacks = new Action<object?>[1];
// Place here subscription
IDisposable[] subscriptions = new IDisposable[1];
// Create callback
callbacks[0] = o =>
{
// File has been modified
Console.WriteLine($"Modified ({token.HasChanged})");
// Create new monitor
var _token = new PollingFilterWatchToken(fileProvider, filterPath, TimeSpan.FromSeconds(0.5));
// Register new token
subscriptions[0] = _token.RegisterChangeCallback(callbacks[0], null);
};
// Register callback
subscriptions[0] = token.RegisterChangeCallback(callbacks[0], null);
// Monitor file changes until key is pressed
Console.WriteLine($"Monitoring file changes \"{filterPath}\". Press key to stop monitoring.");
Console.ReadKey();
subscriptions[0].Dispose();
Full Example
Full example
using Avalanche.FileSystem;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Primitives;
public class pollingfilterwatchtoken
{
public static void Run()
{
{
// <01>
// File pattern to monitor
string filterPath = "**/*.txt";
// Create IFileProvider
IFileProvider fileProvider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
// Create initial token
IChangeToken token = new PollingFilterWatchToken(fileProvider, filterPath, TimeSpan.FromSeconds(0.5));
// Place here callback
Action<object?>[] callbacks = new Action<object?>[1];
// Place here subscription
IDisposable[] subscriptions = new IDisposable[1];
// Create callback
callbacks[0] = o =>
{
// File has been modified
Console.WriteLine($"Modified ({token.HasChanged})");
// Create new monitor
var _token = new PollingFilterWatchToken(fileProvider, filterPath, TimeSpan.FromSeconds(0.5));
// Register new token
subscriptions[0] = _token.RegisterChangeCallback(callbacks[0], null);
};
// Register callback
subscriptions[0] = token.RegisterChangeCallback(callbacks[0], null);
// Monitor file changes until key is pressed
Console.WriteLine($"Monitoring file changes \"{filterPath}\". Press key to stop monitoring.");
Console.ReadKey();
subscriptions[0].Dispose();
// </01>
}
}
}