Progress.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Threading;
  5. using System.Diagnostics;
  6. namespace System
  7. {
  8. /// <summary>
  9. /// Provides an IProgress{T} that invokes callbacks for each reported progress value.
  10. /// </summary>
  11. /// <typeparam name="T">Specifies the type of the progress report value.</typeparam>
  12. /// <remarks>
  13. /// Any handler provided to the constructor or event handlers registered with
  14. /// the <see cref="ProgressChanged"/> event are invoked through a
  15. /// <see cref="System.Threading.SynchronizationContext"/> instance captured
  16. /// when the instance is constructed. If there is no current SynchronizationContext
  17. /// at the time of construction, the callbacks will be invoked on the ThreadPool.
  18. /// </remarks>
  19. public class Progress<T> : IProgress<T>
  20. {
  21. /// <summary>The synchronization context captured upon construction. This will never be null.</summary>
  22. private readonly SynchronizationContext _synchronizationContext;
  23. /// <summary>The handler specified to the constructor. This may be null.</summary>
  24. private readonly Action<T>? _handler;
  25. /// <summary>A cached delegate used to post invocation to the synchronization context.</summary>
  26. private readonly SendOrPostCallback _invokeHandlers;
  27. /// <summary>Initializes the <see cref="Progress{T}"/>.</summary>
  28. public Progress()
  29. {
  30. // Capture the current synchronization context.
  31. // If there is no current context, we use a default instance targeting the ThreadPool.
  32. _synchronizationContext = SynchronizationContext.Current ?? ProgressStatics.DefaultContext;
  33. Debug.Assert(_synchronizationContext != null);
  34. _invokeHandlers = new SendOrPostCallback(InvokeHandlers);
  35. }
  36. /// <summary>Initializes the <see cref="Progress{T}"/> with the specified callback.</summary>
  37. /// <param name="handler">
  38. /// A handler to invoke for each reported progress value. This handler will be invoked
  39. /// in addition to any delegates registered with the <see cref="ProgressChanged"/> event.
  40. /// Depending on the <see cref="System.Threading.SynchronizationContext"/> instance captured by
  41. /// the <see cref="Progress{T}"/> at construction, it's possible that this handler instance
  42. /// could be invoked concurrently with itself.
  43. /// </param>
  44. /// <exception cref="System.ArgumentNullException">The <paramref name="handler"/> is null (Nothing in Visual Basic).</exception>
  45. public Progress(Action<T> handler) : this()
  46. {
  47. _handler = handler ?? throw new ArgumentNullException(nameof(handler));
  48. }
  49. /// <summary>Raised for each reported progress value.</summary>
  50. /// <remarks>
  51. /// Handlers registered with this event will be invoked on the
  52. /// <see cref="System.Threading.SynchronizationContext"/> captured when the instance was constructed.
  53. /// </remarks>
  54. public event EventHandler<T>? ProgressChanged;
  55. /// <summary>Reports a progress change.</summary>
  56. /// <param name="value">The value of the updated progress.</param>
  57. protected virtual void OnReport(T value)
  58. {
  59. // If there's no handler, don't bother going through the sync context.
  60. // Inside the callback, we'll need to check again, in case
  61. // an event handler is removed between now and then.
  62. Action<T>? handler = _handler;
  63. EventHandler<T>? changedEvent = ProgressChanged;
  64. if (handler != null || changedEvent != null)
  65. {
  66. // Post the processing to the sync context.
  67. // (If T is a value type, it will get boxed here.)
  68. _synchronizationContext.Post(_invokeHandlers, value);
  69. }
  70. }
  71. /// <summary>Reports a progress change.</summary>
  72. /// <param name="value">The value of the updated progress.</param>
  73. void IProgress<T>.Report(T value) { OnReport(value); }
  74. /// <summary>Invokes the action and event callbacks.</summary>
  75. /// <param name="state">The progress value.</param>
  76. private void InvokeHandlers(object? state)
  77. {
  78. T value = (T)state!;
  79. Action<T>? handler = _handler;
  80. EventHandler<T>? changedEvent = ProgressChanged;
  81. if (handler != null) handler(value);
  82. if (changedEvent != null) changedEvent(this, value);
  83. }
  84. }
  85. /// <summary>Holds static values for <see cref="Progress{T}"/>.</summary>
  86. /// <remarks>This avoids one static instance per type T.</remarks>
  87. internal static class ProgressStatics
  88. {
  89. /// <summary>A default synchronization context that targets the ThreadPool.</summary>
  90. internal static readonly SynchronizationContext DefaultContext = new SynchronizationContext();
  91. }
  92. }