TaskServices.cs 1.9 KB

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