ActionableProgress.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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> : IProgress<T>, IDisposable
  10. {
  11. /// <summary>
  12. /// The _actions
  13. /// </summary>
  14. private readonly List<Action<T>> _actions = new List<Action<T>>();
  15. public event EventHandler<T> ProgressChanged;
  16. /// <summary>
  17. /// Registers the action.
  18. /// </summary>
  19. /// <param name="action">The action.</param>
  20. public void RegisterAction(Action<T> action)
  21. {
  22. _actions.Add(action);
  23. }
  24. /// <summary>
  25. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  26. /// </summary>
  27. public void Dispose()
  28. {
  29. Dispose(true);
  30. }
  31. /// <summary>
  32. /// Releases unmanaged and - optionally - managed resources.
  33. /// </summary>
  34. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  35. protected virtual void Dispose(bool disposing)
  36. {
  37. if (disposing)
  38. {
  39. _actions.Clear();
  40. }
  41. }
  42. public void Report(T value)
  43. {
  44. if (ProgressChanged != null)
  45. {
  46. ProgressChanged(this, value);
  47. }
  48. foreach (var action in _actions)
  49. {
  50. action(value);
  51. }
  52. }
  53. }
  54. public class SimpleProgress<T> : IProgress<T>
  55. {
  56. public event EventHandler<T> ProgressChanged;
  57. public void Report(T value)
  58. {
  59. if (ProgressChanged != null)
  60. {
  61. ProgressChanged(this, value);
  62. }
  63. }
  64. }
  65. }