Browse Source

Optimize CurrentThreadIsLoopThread on android backend

Max Katz 3 years ago
parent
commit
8100cd3571

+ 16 - 1
src/Android/Avalonia.Android/AndroidThreadingInterface.cs

@@ -14,6 +14,7 @@ namespace Avalonia.Android
     internal sealed class AndroidThreadingInterface : IPlatformThreadingInterface
     {
         private Handler _handler;
+        private static Thread s_uiThread;
 
         public AndroidThreadingInterface()
         {
@@ -76,7 +77,21 @@ namespace Avalonia.Android
             EnsureInvokeOnMainThread(() => Signaled?.Invoke(null));
         }
 
-        public bool CurrentThreadIsLoopThread => Looper.MainLooper.Thread.Equals(Java.Lang.Thread.CurrentThread());
+        public bool CurrentThreadIsLoopThread
+        {
+            get
+            {
+                if (s_uiThread != null)
+                    return s_uiThread == Thread.CurrentThread;
+
+                if (Looper.MainLooper.IsCurrentThread)
+                {
+                    s_uiThread = Thread.CurrentThread;
+                    return true;
+                }
+                return false;
+            }
+        }
         public event Action<DispatcherPriority?> Signaled;
     }
 }

+ 1 - 1
src/Android/Avalonia.Android/Avalonia.Android.csproj

@@ -1,7 +1,7 @@
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <TargetFramework>net6.0-android</TargetFramework>
-    <SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
+    <SupportedOSPlatformVersion>23</SupportedOSPlatformVersion>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <MSBuildEnableWorkloadResolver>true</MSBuildEnableWorkloadResolver>
     <DebugType>portable</DebugType>