RiderFileSystemWatcher.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. namespace Packages.Rider.Editor
  6. {
  7. internal static class RiderFileSystemWatcher
  8. {
  9. public static void InitWatcher(string watchDirectory,
  10. string filter, FileSystemEventHandler onChanged)
  11. {
  12. Task.Run(() =>
  13. {
  14. var watcher = new FileSystemWatcher();
  15. watcher.Path = watchDirectory;
  16. watcher.NotifyFilter = NotifyFilters.LastWrite; //Watch for changes in LastWrite times
  17. watcher.Filter = filter;
  18. watcher.Changed += onChanged;
  19. watcher.Deleted += onChanged;
  20. watcher.EnableRaisingEvents = true;// Begin watching.
  21. return watcher;
  22. }).ContinueWith(task =>
  23. {
  24. try
  25. {
  26. var watcher = task.Result;
  27. AppDomain.CurrentDomain.DomainUnload += (EventHandler) ((_, __) =>
  28. {
  29. watcher.Dispose();
  30. });
  31. }
  32. catch (Exception ex)
  33. {
  34. Debug.LogError(ex);
  35. }
  36. }, TaskScheduler.FromCurrentSynchronizationContext());
  37. }
  38. }
  39. }