ActionableProgress.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Common.Progress
  4. {
  5. /// <summary>
  6. /// Class ActionableProgress
  7. /// </summary>
  8. /// <typeparam name="T"></typeparam>
  9. public class ActionableProgress<T> : Progress<T>, IDisposable
  10. {
  11. /// <summary>
  12. /// The _actions
  13. /// </summary>
  14. private readonly List<Action<T>> _actions = new List<Action<T>>();
  15. /// <summary>
  16. /// Registers the action.
  17. /// </summary>
  18. /// <param name="action">The action.</param>
  19. public void RegisterAction(Action<T> action)
  20. {
  21. _actions.Add(action);
  22. ProgressChanged -= ActionableProgress_ProgressChanged;
  23. ProgressChanged += ActionableProgress_ProgressChanged;
  24. }
  25. /// <summary>
  26. /// Actionables the progress_ progress changed.
  27. /// </summary>
  28. /// <param name="sender">The sender.</param>
  29. /// <param name="e">The e.</param>
  30. void ActionableProgress_ProgressChanged(object sender, T e)
  31. {
  32. foreach (var action in _actions)
  33. {
  34. action(e);
  35. }
  36. }
  37. /// <summary>
  38. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  39. /// </summary>
  40. public void Dispose()
  41. {
  42. Dispose(true);
  43. }
  44. /// <summary>
  45. /// Releases unmanaged and - optionally - managed resources.
  46. /// </summary>
  47. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  48. protected virtual void Dispose(bool disposing)
  49. {
  50. if (disposing)
  51. {
  52. ProgressChanged -= ActionableProgress_ProgressChanged;
  53. _actions.Clear();
  54. }
  55. }
  56. }
  57. }