Browse Source

Optimization for Process.GetCurrentProcess() in Avalonia.Win32.Automation (#17422)

* Use Environment.ProcessId instead of Process.GetCurrentProcess().Id

* revert override of GetPropertyValue

* apply suggestions from review
Easley 1 year ago
parent
commit
bde82e69b5
1 changed files with 13 additions and 1 deletions
  1. 13 1
      src/Windows/Avalonia.Win32.Automation/AutomationNode.cs

+ 13 - 1
src/Windows/Avalonia.Win32.Automation/AutomationNode.cs

@@ -62,6 +62,8 @@ namespace Avalonia.Win32.Automation
 
         private readonly int[] _runtimeId;
 
+        private static readonly int s_pid = GetProcessId();
+
         public AutomationNode(AutomationPeer peer)
         {
             _runtimeId = new int[] { 3, GetHashCode() };
@@ -136,7 +138,7 @@ namespace Avalonia.Win32.Automation
                 UiaPropertyId.LocalizedControlType => InvokeSync(() => Peer.GetLocalizedControlType()),
                 UiaPropertyId.Name => InvokeSync(() => Peer.GetName()),
                 UiaPropertyId.HelpText => InvokeSync(() => Peer.GetHelpText()),
-                UiaPropertyId.ProcessId => Process.GetCurrentProcess().Id,
+                UiaPropertyId.ProcessId => s_pid,
                 UiaPropertyId.RuntimeId => _runtimeId,
                 _ => null,
             };
@@ -355,5 +357,15 @@ namespace Avalonia.Win32.Automation
                 _ => UiaControlTypeId.Custom,
             };
         }
+
+        private static int GetProcessId()
+        {
+#if NET6_0_OR_GREATER
+            return Environment.ProcessId;
+#else
+            using var proccess = Process.GetCurrentProcess();
+            return proccess.Id;
+#endif
+        }
     }
 }