TaskServices.cs 1.9 KB

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