From fab5674f74dcfc728d1e3f6eec9c0c50c9322d1b Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Mon, 5 May 2025 13:37:22 +0900 Subject: [PATCH 1/3] Init --- src/Files.App.CsWin32/NativeMethods.txt | 5 +++ .../Windows/Managers/STATask.cs | 44 +++++++++++++++++++ .../Helpers/Win32/Win32PInvoke.Methods.cs | 9 ---- src/Files.App/Program.cs | 28 +----------- 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index a758d0ef4c51..5e143279b36e 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -269,3 +269,8 @@ HCERTSTORE HCRYPTMSG CERT_QUERY_ENCODING_TYPE CertGetNameString +CreateEvent +SetEvent +CoWaitForMultipleObjects +CWMO_FLAGS +INFINITE diff --git a/src/Files.App.Storage/Windows/Managers/STATask.cs b/src/Files.App.Storage/Windows/Managers/STATask.cs index 742e856e3a84..eb5a588df329 100644 --- a/src/Files.App.Storage/Windows/Managers/STATask.cs +++ b/src/Files.App.Storage/Windows/Managers/STATask.cs @@ -3,6 +3,9 @@ using Microsoft.Extensions.Logging; using Windows.Win32; +using Windows.Win32.Foundation; +using Windows.Win32.System.Com; +using Windows.Win32.Security; namespace Files.App.Storage { @@ -140,5 +143,46 @@ public static Task Run(Func func, ILogger? logger = null) return tcs.Task; } + + public unsafe static Task RunAsSync(Action action) + { + Debug.Assert(Thread.CurrentThread.GetApartmentState() is ApartmentState.STA); + + HANDLE hEventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, true, false, default); + + var tcs = new TaskCompletionSource(); + + Task.Run(() => + { + try + { + action(); + tcs.SetResult(); + } + catch (Exception ex) + { + tcs.SetException(ex); + } + finally + { + PInvoke.SetEvent(hEventHandle); + } + }); + + HANDLE* pEventHandles = stackalloc HANDLE[1]; + pEventHandles[0] = hEventHandle; + uint dwIndex = 0u; + + PInvoke.CoWaitForMultipleObjects( + (uint)CWMO_FLAGS.CWMO_DEFAULT, + PInvoke.INFINITE, + 1u, + pEventHandles, + &dwIndex); + + PInvoke.CloseHandle(hEventHandle); + + return tcs.Task; + } } } diff --git a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs index e52a08638811..0c214c2d2c49 100644 --- a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs +++ b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs @@ -68,15 +68,6 @@ public static extern bool SetEvent( IntPtr hEvent ); - [DllImport("ole32.dll")] - public static extern uint CoWaitForMultipleObjects( - uint dwFlags, - uint dwMilliseconds, - ulong nHandles, - IntPtr[] pHandles, - out uint dwIndex - ); - [DllImport("shell32.dll")] public static extern IntPtr SHBrowseForFolder( ref BROWSEINFO lpbi diff --git a/src/Files.App/Program.cs b/src/Files.App/Program.cs index 6d7807095797..96b16b3af2d0 100644 --- a/src/Files.App/Program.cs +++ b/src/Files.App/Program.cs @@ -9,7 +9,6 @@ using System.Text; using Windows.ApplicationModel.Activation; using Windows.Storage; -using static Files.App.Helpers.Win32PInvoke; namespace Files.App { @@ -21,9 +20,6 @@ namespace Files.App /// internal sealed class Program { - private const uint CWMO_DEFAULT = 0; - private const uint INFINITE = 0xFFFFFFFF; - public static Semaphore? Pool { get; set; } static Program() @@ -250,20 +246,10 @@ private static async void OnActivated(object? sender, AppActivationArguments arg /// public static void RedirectActivationTo(AppInstance keyInstance, AppActivationArguments args) { - IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null); - - Task.Run(() => + STATask.RunAsSync(() => { keyInstance.RedirectActivationToAsync(args).AsTask().Wait(); - SetEvent(eventHandle); }); - - _ = CoWaitForMultipleObjects( - CWMO_DEFAULT, - INFINITE, - 1, - [eventHandle], - out uint handleIndex); } public static void OpenShellCommandInExplorer(string shellCommand, int pid) @@ -273,20 +259,10 @@ public static void OpenShellCommandInExplorer(string shellCommand, int pid) public static void OpenFileFromTile(string filePath) { - IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null); - - Task.Run(() => + STATask.RunAsSync(() => { LaunchHelper.LaunchAppAsync(filePath, null, null).Wait(); - SetEvent(eventHandle); }); - - _ = CoWaitForMultipleObjects( - CWMO_DEFAULT, - INFINITE, - 1, - [eventHandle], - out uint handleIndex); } } } From e7c0917be8f75ce38c12447c86900615e5f09ef7 Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Wed, 20 Aug 2025 06:50:11 +0900 Subject: [PATCH 2/3] Req --- .../Windows/Managers/STATask.cs | 41 ------------------- src/Files.App/Program.cs | 10 +---- 2 files changed, 2 insertions(+), 49 deletions(-) diff --git a/src/Files.App.Storage/Windows/Managers/STATask.cs b/src/Files.App.Storage/Windows/Managers/STATask.cs index eb5a588df329..935180af8b0c 100644 --- a/src/Files.App.Storage/Windows/Managers/STATask.cs +++ b/src/Files.App.Storage/Windows/Managers/STATask.cs @@ -143,46 +143,5 @@ public static Task Run(Func func, ILogger? logger = null) return tcs.Task; } - - public unsafe static Task RunAsSync(Action action) - { - Debug.Assert(Thread.CurrentThread.GetApartmentState() is ApartmentState.STA); - - HANDLE hEventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, true, false, default); - - var tcs = new TaskCompletionSource(); - - Task.Run(() => - { - try - { - action(); - tcs.SetResult(); - } - catch (Exception ex) - { - tcs.SetException(ex); - } - finally - { - PInvoke.SetEvent(hEventHandle); - } - }); - - HANDLE* pEventHandles = stackalloc HANDLE[1]; - pEventHandles[0] = hEventHandle; - uint dwIndex = 0u; - - PInvoke.CoWaitForMultipleObjects( - (uint)CWMO_FLAGS.CWMO_DEFAULT, - PInvoke.INFINITE, - 1u, - pEventHandles, - &dwIndex); - - PInvoke.CloseHandle(hEventHandle); - - return tcs.Task; - } } } diff --git a/src/Files.App/Program.cs b/src/Files.App/Program.cs index 96b16b3af2d0..13e9e9b18465 100644 --- a/src/Files.App/Program.cs +++ b/src/Files.App/Program.cs @@ -246,10 +246,7 @@ private static async void OnActivated(object? sender, AppActivationArguments arg /// public static void RedirectActivationTo(AppInstance keyInstance, AppActivationArguments args) { - STATask.RunAsSync(() => - { - keyInstance.RedirectActivationToAsync(args).AsTask().Wait(); - }); + keyInstance.RedirectActivationToAsync(args).AsTask().Wait(); } public static void OpenShellCommandInExplorer(string shellCommand, int pid) @@ -259,10 +256,7 @@ public static void OpenShellCommandInExplorer(string shellCommand, int pid) public static void OpenFileFromTile(string filePath) { - STATask.RunAsSync(() => - { - LaunchHelper.LaunchAppAsync(filePath, null, null).Wait(); - }); + LaunchHelper.LaunchAppAsync(filePath, null, null).Wait(); } } } From bb62deb33ed2b6571963148b5026e333a9abd60a Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Wed, 20 Aug 2025 06:51:39 +0900 Subject: [PATCH 3/3] Reverted unnecessary changes --- src/Files.App.CsWin32/NativeMethods.txt | 5 ----- src/Files.App.Storage/Windows/Managers/STATask.cs | 3 --- 2 files changed, 8 deletions(-) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index 5e143279b36e..a758d0ef4c51 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -269,8 +269,3 @@ HCERTSTORE HCRYPTMSG CERT_QUERY_ENCODING_TYPE CertGetNameString -CreateEvent -SetEvent -CoWaitForMultipleObjects -CWMO_FLAGS -INFINITE diff --git a/src/Files.App.Storage/Windows/Managers/STATask.cs b/src/Files.App.Storage/Windows/Managers/STATask.cs index 935180af8b0c..742e856e3a84 100644 --- a/src/Files.App.Storage/Windows/Managers/STATask.cs +++ b/src/Files.App.Storage/Windows/Managers/STATask.cs @@ -3,9 +3,6 @@ using Microsoft.Extensions.Logging; using Windows.Win32; -using Windows.Win32.Foundation; -using Windows.Win32.System.Com; -using Windows.Win32.Security; namespace Files.App.Storage {