TaskServices.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !NO_TPL
  5. using System.ComponentModel;
  6. using System.Reactive.PlatformServices;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Reactive
  10. {
  11. internal static class TaskHelpers
  12. {
  13. private static Lazy<ITaskServices> s_services = new Lazy<ITaskServices>(Initialize);
  14. private static ITaskServices Initialize()
  15. {
  16. #pragma warning disable CS0618 // Type or member is obsolete
  17. return PlatformEnlightenmentProvider.Current.GetService<ITaskServices>() ?? new DefaultTaskServices();
  18. #pragma warning restore CS0618 // Type or member is obsolete
  19. }
  20. public static bool TrySetCanceled<T>(this TaskCompletionSource<T> tcs, CancellationToken token)
  21. {
  22. return s_services.Value.TrySetCanceled(tcs, token);
  23. }
  24. }
  25. }
  26. namespace System.Reactive.PlatformServices
  27. {
  28. /// <summary>
  29. /// (Infrastructure) Services to leverage evolving TPL Task APIs.
  30. /// </summary>
  31. /// <remarks>
  32. /// This type is used by the Rx infrastructure and not meant for public consumption or implementation.
  33. /// No guarantees are made about forward compatibility of the type's functionality and its usage.
  34. /// </remarks>
  35. [EditorBrowsable(EditorBrowsableState.Never)]
  36. public interface ITaskServices
  37. {
  38. /// <summary>
  39. /// Attempts to transition the underlying Task{T} into the Canceled state.
  40. /// </summary>
  41. /// <typeparam name="T">Type of the result of the underlying task.</typeparam>
  42. /// <param name="tcs">Task completion source whose underlying task to transition into the Canceled state.</param>
  43. /// <param name="token">Cancellation token that triggered the cancellation.</param>
  44. /// <returns>True if the operation was successful; false if the operation was unsuccessful or the object has already been disposed.</returns>
  45. bool TrySetCanceled<T>(TaskCompletionSource<T> tcs, CancellationToken token);
  46. }
  47. }
  48. #endif