static void Main(string[] args) { Console.Title = "Hello World With Events!"; Console.WriteLine("Starting up Swatcher..."); //First, we need to create a configuration object to tell Swatcher how to run. var config = CreateConfiguration(); var swatcher = new Swatcher(config); swatcher.ItemChanged += SwatcherOnChanged; swatcher.ItemCreated += SwatcherOnCreated; swatcher.ItemDeleted += SwatcherOnDeleted; swatcher.ItemRenamed += SwatcherOnRenamed; swatcher.Start().GetAwaiter().GetResult(); Console.WriteLine("Swatcher has started and is listening for events..."); Console.ReadKey(); //Shutting down swatcher.Stop().GetAwaiter().GetResult(); //Unsubscribe after you Stop if you're done with the component. swatcher.ItemChanged -= SwatcherOnChanged; swatcher.ItemCreated -= SwatcherOnCreated; swatcher.ItemDeleted -= SwatcherOnDeleted; swatcher.ItemRenamed -= SwatcherOnRenamed; //cleanup swatcher.Dispose(); }
public void Dispose() { _swatcher.Stop().GetAwaiter().GetResult(); _swatcher.Dispose(); }
private static void Main(string[] args) { Console.Title = "Reactive Hello World"; Console.WriteLine("Starting up Swatcher..."); var disposables = new CompositeDisposable(); var config = CreateConfiguration(); var swatcher = new Swatcher(config); //NOTE: you will get logging message output to the console if you enabled logging in the Swatcher config. //put your business logic in the handlers below and you're good to go. swatcher.Changed.Subscribe(x => { if (config.LoggingEnabled) { return; } Console.WriteLine($"[Changed] Name: {x.Name}, OccurredAt:{x.TimeOccurred.ToLocalTime()}"); }) .DisposeWith(disposables); swatcher.Created.Subscribe(x => { if (config.LoggingEnabled) { return; } var processingTime = x.Duration.Seconds > 1 ? $"{x.Duration.Seconds} sec" : $"{x.Duration.Milliseconds} ms"; Console.WriteLine( $"[Created] Name: {x.Name}, OccurredAt:{x.TimeOccurred.ToLocalTime()}, ProcessingTime: {processingTime}"); }) .DisposeWith(disposables); swatcher.Deleted.Subscribe(x => { if (config.LoggingEnabled) { return; } Console.WriteLine($"[Deleted] Name: {x.Name}, OccurredAt:{x.TimeOccurred.ToLocalTime()}"); }) .DisposeWith(disposables); swatcher.Renamed.Subscribe(x => { if (config.LoggingEnabled) { Console.WriteLine( $"[Renamed] OldName: {x.OldName}, Name: {x.Name}, OccurredAt:{x.TimeOccurred.ToLocalTime()}"); } }) .DisposeWith(disposables); swatcher.Start().GetAwaiter().GetResult(); Console.WriteLine("Swatcher has started and is listening for events..."); Console.ReadKey(); //Do stuff in your monitored folder.... //Shutting down swatcher.Stop().GetAwaiter().GetResult(); //Unsubscribe after you Stop if you're done with the component. disposables.Dispose(); swatcher.Dispose(); //voila! contrived, but pretty easy, eh? }