TestLongRunningScheduler.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Reactive.Concurrency;
  4. using System.Reactive.Disposables;
  5. using System.Threading;
  6. namespace ReactiveTests
  7. {
  8. class TestLongRunningScheduler : IScheduler, ISchedulerLongRunning, IServiceProvider
  9. {
  10. private Action<ManualResetEvent> _setStart;
  11. private Action<ManualResetEvent> _setEnd;
  12. private Action<Exception> _setException;
  13. public TestLongRunningScheduler(Action<ManualResetEvent> setStart, Action<ManualResetEvent> setEnd)
  14. : this(setStart, setEnd, null)
  15. {
  16. }
  17. public TestLongRunningScheduler(Action<ManualResetEvent> setStart, Action<ManualResetEvent> setEnd, Action<Exception> setException)
  18. {
  19. _setStart = setStart;
  20. _setEnd = setEnd;
  21. _setException = setException;
  22. }
  23. public DateTimeOffset Now
  24. {
  25. get { return DateTimeOffset.Now; }
  26. }
  27. public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
  28. {
  29. throw new NotImplementedException();
  30. }
  31. public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
  32. {
  33. throw new NotImplementedException();
  34. }
  35. public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
  36. {
  37. throw new NotImplementedException();
  38. }
  39. public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action)
  40. {
  41. var d = new BooleanDisposable();
  42. var eb = new ManualResetEvent(false);
  43. _setStart(eb);
  44. var ee = new ManualResetEvent(false);
  45. _setEnd(ee);
  46. new Thread(() =>
  47. {
  48. eb.Set();
  49. try
  50. {
  51. action(state, d);
  52. }
  53. catch (Exception ex)
  54. {
  55. if (_setException == null)
  56. throw;
  57. _setException(ex);
  58. }
  59. finally
  60. {
  61. ee.Set();
  62. }
  63. }).Start();
  64. return d;
  65. }
  66. object IServiceProvider.GetService(Type serviceType)
  67. {
  68. if (serviceType == typeof(ISchedulerLongRunning))
  69. return this;
  70. return null;
  71. }
  72. }
  73. }