Scheduler.Wrappers.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. namespace System.Reactive.Concurrency
  5. {
  6. public static partial class Scheduler
  7. {
  8. /// <summary>
  9. /// Returns a scheduler that represents the original scheduler, without any of its interface-based optimizations (e.g. long running scheduling).
  10. /// </summary>
  11. /// <param name="scheduler">Scheduler to disable all optimizations for.</param>
  12. /// <returns>Proxy to the original scheduler but without any optimizations enabled.</returns>
  13. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is <c>null</c>.</exception>
  14. public static IScheduler DisableOptimizations(this IScheduler scheduler)
  15. {
  16. if (scheduler == null)
  17. throw new ArgumentNullException(nameof(scheduler));
  18. return new DisableOptimizationsScheduler(scheduler);
  19. }
  20. /// <summary>
  21. /// Returns a scheduler that represents the original scheduler, without the specified set of interface-based optimizations (e.g. long running scheduling).
  22. /// </summary>
  23. /// <param name="scheduler">Scheduler to disable the specified optimizations for.</param>
  24. /// <param name="optimizationInterfaces">Types of the optimization interfaces that have to be disabled.</param>
  25. /// <returns>Proxy to the original scheduler but without the specified optimizations enabled.</returns>
  26. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="optimizationInterfaces"/> is <c>null</c>.</exception>
  27. public static IScheduler DisableOptimizations(this IScheduler scheduler, params Type[] optimizationInterfaces)
  28. {
  29. if (scheduler == null)
  30. throw new ArgumentNullException(nameof(scheduler));
  31. if (optimizationInterfaces == null)
  32. throw new ArgumentNullException(nameof(optimizationInterfaces));
  33. return new DisableOptimizationsScheduler(scheduler, optimizationInterfaces);
  34. }
  35. /// <summary>
  36. /// Returns a scheduler that wraps the original scheduler, adding exception handling for scheduled actions.
  37. /// </summary>
  38. /// <typeparam name="TException">Type of the exception to check for.</typeparam>
  39. /// <param name="scheduler">Scheduler to apply an exception filter for.</param>
  40. /// <param name="handler">Handler that's run if an exception is caught. The exception will be rethrown if the handler returns false.</param>
  41. /// <returns>Wrapper around the original scheduler, enforcing exception handling.</returns>
  42. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="handler"/> is <c>null</c>.</exception>
  43. public static IScheduler Catch<TException>(this IScheduler scheduler, Func<TException, bool> handler)
  44. where TException : Exception
  45. {
  46. if (scheduler == null)
  47. throw new ArgumentNullException(nameof(scheduler));
  48. if (handler == null)
  49. throw new ArgumentNullException(nameof(handler));
  50. return new CatchScheduler<TException>(scheduler, handler);
  51. }
  52. }
  53. }