Browse Source

A few more changes

Bart De Smet 8 năm trước cách đây
mục cha
commit
d6794e1b2c

+ 27 - 29
Rx.NET/Source/src/System.Reactive/Platforms/Windows/Concurrency/CoreDispatcherScheduler.cs

@@ -13,7 +13,7 @@ using Windows.UI.Xaml;
 namespace System.Reactive.Concurrency
 {
     /// <summary>
-    /// Represents an object that schedules units of work on a Windows.UI.Core.CoreDispatcher.
+    /// Represents an object that schedules units of work on a <see cref="Windows.UI.Core.CoreDispatcher"/>.
     /// </summary>
     /// <remarks>
     /// This scheduler type is typically used indirectly through the <see cref="System.Reactive.Linq.DispatcherObservable.ObserveOnDispatcher{TSource}(IObservable{TSource})"/> and <see cref="System.Reactive.Linq.DispatcherObservable.SubscribeOnDispatcher{TSource}(IObservable{TSource})"/> methods that use the current Dispatcher.
@@ -21,40 +21,37 @@ namespace System.Reactive.Concurrency
     [CLSCompliant(false)]
     public sealed class CoreDispatcherScheduler : LocalScheduler, ISchedulerPeriodic
     {
-        private readonly CoreDispatcher _dispatcher;
-        private readonly CoreDispatcherPriority _priority;
-
         /// <summary>
-        /// Constructs a CoreDispatcherScheduler that schedules units of work on the given Windows.UI.Core.CoreDispatcher.
+        /// Constructs a <see cref="CoreDispatcherScheduler"/> that schedules units of work on the given <see cref="Windows.UI.Core.CoreDispatcher"/>.
         /// </summary>
         /// <param name="dispatcher">Dispatcher to schedule work on.</param>
-        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is <c>null</c>.</exception>
         public CoreDispatcherScheduler(CoreDispatcher dispatcher)
         {
             if (dispatcher == null)
                 throw new ArgumentNullException(nameof(dispatcher));
 
-            _dispatcher = dispatcher;
-            _priority = CoreDispatcherPriority.Normal;
+            Dispatcher = dispatcher;
+            Priority = CoreDispatcherPriority.Normal;
         }
 
         /// <summary>
-        /// Constructs a CoreDispatcherScheduler that schedules units of work on the given Windows.UI.Core.CoreDispatcher with the given priority.
+        /// Constructs a <see cref="CoreDispatcherScheduler"/> that schedules units of work on the given <see cref="Windows.UI.Core.CoreDispatcher"/> with the given priority.
         /// </summary>
         /// <param name="dispatcher">Dispatcher to schedule work on.</param>
         /// <param name="priority">Priority for scheduled units of work.</param>
-        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is <c>null</c>.</exception>
         public CoreDispatcherScheduler(CoreDispatcher dispatcher, CoreDispatcherPriority priority)
         {
             if (dispatcher == null)
                 throw new ArgumentNullException(nameof(dispatcher));
 
-            _dispatcher = dispatcher;
-            _priority = priority;
+            Dispatcher = dispatcher;
+            Priority = priority;
         }
 
         /// <summary>
-        /// Gets the scheduler that schedules work on the Windows.UI.Core.CoreDispatcher associated with the current Window.
+        /// Gets the scheduler that schedules work on the <see cref="Windows.UI.Core.CoreDispatcher"/> associated with the current Window.
         /// </summary>
         public static CoreDispatcherScheduler Current
         {
@@ -69,20 +66,14 @@ namespace System.Reactive.Concurrency
         }
 
         /// <summary>
-        /// Gets the Windows.UI.Core.CoreDispatcher associated with the CoreDispatcherScheduler.
+        /// Gets the <see cref="Windows.UI.Core.CoreDispatcher"/> associated with the <see cref="CoreDispatcherScheduler"/>.
         /// </summary>
-        public CoreDispatcher Dispatcher
-        {
-            get { return _dispatcher; }
-        }
+        public CoreDispatcher Dispatcher { get; }
 
         /// <summary>
         /// Gets the priority at which work is scheduled.
         /// </summary>
-        public CoreDispatcherPriority Priority
-        {
-            get { return _priority; }
-        }
+        public CoreDispatcherPriority Priority { get; }
 
         /// <summary>
         /// Schedules an action to be executed on the dispatcher.
@@ -91,7 +82,7 @@ namespace System.Reactive.Concurrency
         /// <param name="state">State passed to the action to be executed.</param>
         /// <param name="action">Action to be executed.</param>
         /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
         public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
         {
             if (action == null)
@@ -99,7 +90,7 @@ namespace System.Reactive.Concurrency
 
             var d = new SingleAssignmentDisposable();
 
-            var res = _dispatcher.RunAsync(_priority, () =>
+            var res = Dispatcher.RunAsync(Priority, () =>
             {
                 if (!d.IsDisposed)
                 {
@@ -140,14 +131,14 @@ namespace System.Reactive.Concurrency
         }
 
         /// <summary>
-        /// Schedules an action to be executed after dueTime on the dispatcher, using a Windows.UI.Xaml.DispatcherTimer object.
+        /// Schedules an action to be executed after <paramref name="dueTime"/> on the dispatcher, using a <see cref="Windows.UI.Xaml.DispatcherTimer"/> object.
         /// </summary>
         /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
         /// <param name="state">State passed to the action to be executed.</param>
         /// <param name="action">Action to be executed.</param>
         /// <param name="dueTime">Relative time after which to execute the action.</param>
         /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
         public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
         {
             if (action == null)
@@ -155,8 +146,15 @@ namespace System.Reactive.Concurrency
 
             var dt = Scheduler.Normalize(dueTime);
             if (dt.Ticks == 0)
+            {
                 return Schedule(state, action);
+            }
 
+            return ScheduleSlow(state, dt, action);
+        }
+
+        private IDisposable ScheduleSlow<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
+        {
             var d = new MultipleAssignmentDisposable();
 
             var timer = new DispatcherTimer();
@@ -178,7 +176,7 @@ namespace System.Reactive.Concurrency
                 }
             };
 
-            timer.Interval = dt;
+            timer.Interval = dueTime;
             timer.Start();
 
             d.Disposable = Disposable.Create(() =>
@@ -195,14 +193,14 @@ namespace System.Reactive.Concurrency
         }
 
         /// <summary>
-        /// Schedules a periodic piece of work on the dispatcher, using a Windows.UI.Xaml.DispatcherTimer object.
+        /// Schedules a periodic piece of work on the dispatcher, using a <see cref="Windows.UI.Xaml.DispatcherTimer"/> object.
         /// </summary>
         /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
         /// <param name="state">Initial state passed to the action upon the first iteration.</param>
         /// <param name="period">Period for running the work periodically.</param>
         /// <param name="action">Action to be executed, potentially updating the state.</param>
         /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
         /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than <see cref="TimeSpan.Zero"/>.</exception>
         public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
         {

+ 34 - 51
Rx.NET/Source/src/System.Reactive/Platforms/Windows/Concurrency/DispatcherScheduler.cs

@@ -20,16 +20,7 @@ namespace System.Reactive.Concurrency
         /// Gets the scheduler that schedules work on the current <see cref="System.Windows.Threading.Dispatcher"/>.
         /// </summary>
         [Obsolete(Constants_WindowsThreading.OBSOLETE_INSTANCE_PROPERTY)]
-        public static DispatcherScheduler Instance
-        {
-            get
-            {
-                return new DispatcherScheduler(
-
-                    System.Windows.Threading.Dispatcher.CurrentDispatcher
-                );
-            }
-        }
+        public static DispatcherScheduler Instance => new DispatcherScheduler(System.Windows.Threading.Dispatcher.CurrentDispatcher);
 
         /// <summary>
         /// Gets the scheduler that schedules work on the <see cref="System.Windows.Threading.Dispatcher"/> for the current thread.
@@ -43,61 +34,48 @@ namespace System.Reactive.Concurrency
                     throw new InvalidOperationException(Strings_WindowsThreading.NO_DISPATCHER_CURRENT_THREAD);
 
                 return new DispatcherScheduler(dispatcher);
-
             }
         }
 
-        System.Windows.Threading.Dispatcher _dispatcher;
-        System.Windows.Threading.DispatcherPriority _priority;
-
-
         /// <summary>
-        /// Constructs a DispatcherScheduler that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/>.
+        /// Constructs a <see cref="DispatcherScheduler"/> that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/>.
         /// </summary>
-        /// <param name="dispatcher">Dispatcher to schedule work on.</param>
-        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is null.</exception>
+        /// <param name="dispatcher"><see cref="DispatcherScheduler"/> to schedule work on.</param>
+        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is <c>null</c>.</exception>
         public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher)
         {
             if (dispatcher == null)
                 throw new ArgumentNullException(nameof(dispatcher));
 
-            _dispatcher = dispatcher;
-            _priority = Windows.Threading.DispatcherPriority.Normal;
+            Dispatcher = dispatcher;
+            Priority = Windows.Threading.DispatcherPriority.Normal;
 
         }
         
         /// <summary>
-        /// Constructs a DispatcherScheduler that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/> at the given priority.
+        /// Constructs a <see cref="DispatcherScheduler"/> that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/> at the given priority.
         /// </summary>
-        /// <param name="dispatcher">Dispatcher to schedule work on.</param>
+        /// <param name="dispatcher"><see cref="DispatcherScheduler"/> to schedule work on.</param>
         /// <param name="priority">Priority at which units of work are scheduled.</param>
-        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is <c>null</c>.</exception>
         public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher, System.Windows.Threading.DispatcherPriority priority)
         {
             if (dispatcher == null)
                 throw new ArgumentNullException(nameof(dispatcher));
 
-            _dispatcher = dispatcher;
-            _priority = priority;
+            Dispatcher = dispatcher;
+            Priority = priority;
         }
 
-
         /// <summary>
-        /// Gets the <see cref="System.Windows.Threading.Dispatcher"/> associated with the DispatcherScheduler.
+        /// Gets the <see cref="System.Windows.Threading.Dispatcher"/> associated with the <see cref="DispatcherScheduler"/>.
         /// </summary>
-        public System.Windows.Threading.Dispatcher Dispatcher
-        {
-            get { return _dispatcher; }
-        }
+        public System.Windows.Threading.Dispatcher Dispatcher { get; }
         
         /// <summary>
         /// Gets the priority at which work items will be dispatched.
         /// </summary>
-        public System.Windows.Threading.DispatcherPriority Priority
-        {
-            get { return _priority; }
-        }
-
+        public System.Windows.Threading.DispatcherPriority Priority { get; }
 
         /// <summary>
         /// Schedules an action to be executed on the dispatcher.
@@ -106,7 +84,7 @@ namespace System.Reactive.Concurrency
         /// <param name="state">State passed to the action to be executed.</param>
         /// <param name="action">Action to be executed.</param>
         /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
         public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
         {
             if (action == null)
@@ -114,27 +92,29 @@ namespace System.Reactive.Concurrency
 
             var d = new SingleAssignmentDisposable();
 
-            _dispatcher.BeginInvoke(
+            Dispatcher.BeginInvoke(
                 new Action(() =>
                 {
                     if (!d.IsDisposed)
+                    {
                         d.Disposable = action(this, state);
-                })
-                , _priority
+                    }
+                }),
+                Priority
             );
 
             return d;
         }
 
         /// <summary>
-        /// Schedules an action to be executed after dueTime on the dispatcher, using a <see cref="System.Windows.Threading.DispatcherTimer"/> object.
+        /// Schedules an action to be executed after <paramref name="dueTime"/> on the dispatcher, using a <see cref="System.Windows.Threading.DispatcherTimer"/> object.
         /// </summary>
         /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
         /// <param name="state">State passed to the action to be executed.</param>
         /// <param name="action">Action to be executed.</param>
         /// <param name="dueTime">Relative time after which to execute the action.</param>
         /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
         public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
         {
             if (action == null)
@@ -142,13 +122,18 @@ namespace System.Reactive.Concurrency
 
             var dt = Scheduler.Normalize(dueTime);
             if (dt.Ticks == 0)
+            {
                 return Schedule(state, action);
+            }
+
+            return ScheduleSlow(state, dt, action);
+        }
 
+        private IDisposable ScheduleSlow<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
+        {
             var d = new MultipleAssignmentDisposable();
 
-            var timer = new System.Windows.Threading.DispatcherTimer(
-                _priority, _dispatcher
-            );
+            var timer = new System.Windows.Threading.DispatcherTimer(Priority, Dispatcher);
 
             timer.Tick += (s, e) =>
             {
@@ -167,7 +152,7 @@ namespace System.Reactive.Concurrency
                 }
             };
 
-            timer.Interval = dt;
+            timer.Interval = dueTime;
             timer.Start();
 
             d.Disposable = Disposable.Create(() =>
@@ -191,7 +176,7 @@ namespace System.Reactive.Concurrency
         /// <param name="period">Period for running the work periodically.</param>
         /// <param name="action">Action to be executed, potentially updating the state.</param>
         /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
-        /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
+        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
         /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than <see cref="TimeSpan.Zero"/>.</exception>
         public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
         {
@@ -200,9 +185,7 @@ namespace System.Reactive.Concurrency
             if (action == null)
                 throw new ArgumentNullException(nameof(action));
 
-            var timer = new System.Windows.Threading.DispatcherTimer(
-                _priority, _dispatcher
-            );
+            var timer = new System.Windows.Threading.DispatcherTimer(Priority, Dispatcher);
 
             var state1 = state;
 
@@ -226,4 +209,4 @@ namespace System.Reactive.Concurrency
         }
     }
 }
-#endif
+#endif