Scheduler.Wrappers.cs 3.2 KB

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