ActionableProgress.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. GC.SuppressFinalize(this);
  31. }
  32. /// <summary>
  33. /// Releases unmanaged and - optionally - managed resources.
  34. /// </summary>
  35. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  36. protected virtual void Dispose(bool disposing)
  37. {
  38. if (disposing)
  39. {
  40. _actions.Clear();
  41. }
  42. }
  43. public void Report(T value)
  44. {
  45. if (ProgressChanged != null)
  46. {
  47. ProgressChanged(this, value);
  48. }
  49. foreach (var action in _actions)
  50. {
  51. action(value);
  52. }
  53. }
  54. }
  55. public class SimpleProgress<T> : IProgress<T>
  56. {
  57. public event EventHandler<T> ProgressChanged;
  58. public void Report(T value)
  59. {
  60. if (ProgressChanged != null)
  61. {
  62. ProgressChanged(this, value);
  63. }
  64. }
  65. }
  66. }