Przeglądaj źródła

Updates for .NET 5

Claire Novotny 5 lat temu
rodzic
commit
cd8ff91fc9

+ 3 - 3
Rx.NET/Source/Directory.build.targets

@@ -20,12 +20,12 @@
     <DefineConstants>$(DefineConstants);HAS_WINFORMS;HAS_WINRT;HAS_DISPATCHER;HAS_REMOTING;DESKTOPCLR</DefineConstants>
   </PropertyGroup>
   <PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0'">
-    <TargetPlatformVersion>10.0.17763.0</TargetPlatformVersion>
+    <TargetPlatformVersion>10.0.19041.0</TargetPlatformVersion>
     <TargetPlatformMinVersion>10.0.15063.0</TargetPlatformMinVersion>
     <DefineConstants>$(DefineConstants);HAS_WINRT;NO_CODE_COVERAGE_ATTRIBUTE;NO_SERIALIZABLE;CRIPPLED_REFLECTION;NO_THREAD;NO_TRACE;WINDOWS</DefineConstants>
   </PropertyGroup>
   <PropertyGroup Condition="'$(TargetFramework)' == 'uap10.0.16299'">
-    <DefineConstants>$(DefineConstants);HAS_WINRT;WINDOWS</DefineConstants>
+    <DefineConstants>$(DefineConstants);HAS_WINRT;WINDOWS;HAS_OS_XAML</DefineConstants>
     <TargetPlatformVersion>10.0.19041.0</TargetPlatformVersion>
   </PropertyGroup>
   <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.1'">
@@ -35,7 +35,7 @@
     <DefineConstants>$(DefineConstants);HAS_WINRT;HAS_WINFORMS;HAS_DISPATCHER;DESKTOPCLR</DefineConstants>
   </PropertyGroup>
   <PropertyGroup Condition="$(TargetFramework.StartsWith('net5.0-windows'))">
-    <DefineConstants>$(DefineConstants);HAS_TRACE;HAS_WINRT;PREFER_ASYNC;HAS_TPL46;NO_REMOTING;HAS_WINFORMS;HAS_DISPATCHER;DESKTOPCLR;WINDOWS</DefineConstants>
+    <DefineConstants>$(DefineConstants);HAS_TRACE;HAS_WINRT;PREFER_ASYNC;HAS_TPL46;NO_REMOTING;HAS_WINFORMS;HAS_DISPATCHER;DESKTOPCLR;WINDOWS;CSWINRT</DefineConstants>
   </PropertyGroup>
   <PropertyGroup Condition="'$(TargetFramework)' == 'net5.0'">
     <DefineConstants>$(DefineConstants);HAS_TRACE;PREFER_ASYNC;HAS_TPL46;NO_REMOTING</DefineConstants>

+ 1 - 1
Rx.NET/Source/src/System.Reactive/Internal/ReflectionUtils.cs

@@ -71,7 +71,7 @@ namespace System.Reactive
 
             isWinRT = false;
 
-#if HAS_WINRT && !NET5_0_WINDOWS10_0_19041
+#if HAS_WINRT && !CSWINRT
             if (addMethod.ReturnType == typeof(EventRegistrationToken))
             {
                 isWinRT = true;

+ 2 - 2
Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/ControlScheduler.cs

@@ -95,7 +95,7 @@ namespace System.Reactive.Concurrency
             {
                 var d = new MultipleAssignmentDisposable();
 
-                var timer = new Windows.Forms.Timer();
+                var timer = new System.Windows.Forms.Timer();
 
                 timer.Tick += (s, e) =>
                 {
@@ -174,7 +174,7 @@ namespace System.Reactive.Concurrency
 
             var createTimer = new Func<IScheduler, TState, IDisposable>((scheduler1, state1) =>
             {
-                var timer = new Windows.Forms.Timer();
+                var timer = new System.Windows.Forms.Timer();
 
                 timer.Tick += (s, e) =>
                 {

+ 34 - 8
Rx.NET/Source/src/System.Reactive/Platforms/UWP/Concurrency/CoreDispatcherScheduler.cs

@@ -6,8 +6,9 @@
 using System.Reactive.Disposables;
 using System.Runtime.ExceptionServices;
 using System.Threading;
+using Windows.System;
 using Windows.UI.Core;
-#if !NET5_0_WINDOWS10_0_19041
+#if HAS_OS_XAML
 using Windows.UI.Xaml;
 #endif
 namespace System.Reactive.Concurrency
@@ -29,7 +30,7 @@ namespace System.Reactive.Concurrency
         public CoreDispatcherScheduler(CoreDispatcher dispatcher)
         {
             Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
-            Priority = CoreDispatcherPriority.Normal;
+            Priority = CoreDispatcherPriority.Normal;           
         }
 
         /// <summary>
@@ -66,6 +67,8 @@ namespace System.Reactive.Concurrency
         /// </summary>
         public CoreDispatcher Dispatcher { get; }
 
+        private DispatcherQueue? _dispatcherQueue;
+
         /// <summary>
         /// Gets the priority at which work is scheduled.
         /// </summary>
@@ -109,10 +112,10 @@ namespace System.Reactive.Concurrency
                         // For scheduler implementation guidance rules, see TaskPoolScheduler.cs
                         // in System.Reactive.PlatformServices\Reactive\Concurrency.
                         //
-                        var timer = new DispatcherTimer
-                        {
-                            Interval = TimeSpan.Zero
-                        };
+                        
+                        var timer = CreateDispatcherQueue().CreateTimer();
+                        timer.Interval = TimeSpan.Zero;
+
                         timer.Tick += (o, e) =>
                         {
                             timer.Stop();
@@ -130,6 +133,28 @@ namespace System.Reactive.Concurrency
             );
         }
 
+        private DispatcherQueue CreateDispatcherQueue()
+        {
+            if(_dispatcherQueue != null)
+            {
+                return _dispatcherQueue;
+            }
+
+            if(Dispatcher.HasThreadAccess)
+            {
+                _dispatcherQueue = DispatcherQueue.GetForCurrentThread();
+                return _dispatcherQueue;
+            }
+
+            // We're on a different thread, get it from the right one
+            Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
+            {
+                _dispatcherQueue = DispatcherQueue.GetForCurrentThread();
+            }).GetAwaiter().GetResult(); // This is a synchronous call and we need the result to proceed
+
+            return _dispatcherQueue!;
+        }
+
         /// <summary>
         /// Schedules an action to be executed after <paramref name="dueTime"/> on the dispatcher, using a <see cref="DispatcherTimer"/> object.
         /// </summary>
@@ -159,7 +184,8 @@ namespace System.Reactive.Concurrency
         {
             var d = new MultipleAssignmentDisposable();
 
-            var timer = new DispatcherTimer();
+
+            var timer = CreateDispatcherQueue().CreateTimer();
 
             timer.Tick += (o, e) =>
             {
@@ -220,7 +246,7 @@ namespace System.Reactive.Concurrency
                 throw new ArgumentNullException(nameof(action));
             }
 
-            var timer = new DispatcherTimer();
+            var timer = CreateDispatcherQueue().CreateTimer();
 
             var state1 = state;
 

+ 3 - 3
Rx.NET/Source/src/System.Reactive/Platforms/UWP/Linq/CoreDispatcherObservable.cs

@@ -6,7 +6,7 @@
 using System.Reactive.Concurrency;
 using Windows.UI.Core;
 
-#if !NET5_0_WINDOWS10_0_19041
+#if HAS_OS_XAML
 using Windows.UI.Xaml;
 #endif
 
@@ -67,7 +67,7 @@ namespace System.Reactive.Linq
             return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher, priority));
         }
 
-        #if !NET5_0_WINDOWS10_0_19041
+#if HAS_OS_XAML
         /// <summary>
         /// Wraps the source sequence in order to run its observer callbacks on the dispatcher associated with the specified object.
         /// </summary>
@@ -209,7 +209,7 @@ namespace System.Reactive.Linq
             return Synchronization.SubscribeOn(source, new CoreDispatcherScheduler(dispatcher, priority));
         }
 
-#if !NET5_0_WINDOWS10_0_19041
+#if HAS_OS_XAML
         /// <summary>
         /// Wraps the source sequence in order to run its subscription and unsubscription logic on the dispatcher associated with the specified object.
         /// </summary>

+ 3 - 3
Rx.NET/Source/src/System.Reactive/System.Reactive.csproj

@@ -6,10 +6,10 @@
     <Description>Reactive Extensions (Rx) for .NET</Description>
   </PropertyGroup>
 
-  <PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
+  <PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1' or $(TargetFramework.StartsWith('net5.0-windows'))">
     <UseWPF>true</UseWPF>
     <UseWindowsForms>true</UseWindowsForms>
-    <IncludeBuildOutput>false</IncludeBuildOutput>
+    <IncludeBuildOutput Condition="'$(TargetFramework)' == 'netcoreapp3.1'">false</IncludeBuildOutput>
   </PropertyGroup>
 
   <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
@@ -57,7 +57,7 @@
     <Reference Include="WindowsBase" />
   </ItemGroup>
 
-  <ItemGroup Condition=" '$(TargetFramework)' == 'net46' or '$(TargetFramework)' == 'netcoreapp3.1'">
+  <ItemGroup Condition=" '$(TargetFramework)' == 'net46' or '$(TargetFramework)' == 'netcoreapp3.1' or $(TargetFramework.StartsWith('net5.0-windows'))">
     <Compile Include="Platforms\Desktop\**\*.cs" />
   </ItemGroup>
 

+ 1 - 1
Rx.NET/Source/tests/Tests.System.Reactive/DispatcherHelpers.cs

@@ -2,7 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT License.
 // See the LICENSE file in the project root for more information. 
 
-#if NETCOREAPP2_1 || NET46 || NETCOREAPP3_1 || NET5_0_WINDOWS10_0_19041
+#if NETCOREAPP2_1 || NET46 || NETCOREAPP3_1 || CSWINRT
 using System.Threading;
 #endif
 #if HAS_DISPATCHER