Skip to content

Commit eca313e

Browse files
committed
Init
1 parent 5aa072c commit eca313e

File tree

4 files changed

+51
-35
lines changed

4 files changed

+51
-35
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,8 @@ QITIPF_FLAGS
225225
GetKeyboardState
226226
MapVirtualKey
227227
GetKeyboardLayout
228+
CreateEvent
229+
SetEvent
230+
CoWaitForMultipleObjects
231+
CWMO_FLAGS
232+
INFINITE

src/Files.App.Storage/Storables/WindowsStorage/STATask.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Licensed under the MIT License.
33

44
using Windows.Win32;
5+
using Windows.Win32.Foundation;
6+
using Windows.Win32.System.Com;
7+
using Windows.Win32.Security;
58

69
namespace Files.App.Storage
710
{
@@ -143,5 +146,46 @@ public static Task Run(Func<Task> func)
143146

144147
return tcs.Task;
145148
}
149+
150+
public unsafe static Task RunAsSync(Action action)
151+
{
152+
Debug.Assert(Thread.CurrentThread.GetApartmentState() is ApartmentState.STA);
153+
154+
HANDLE hEventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, true, false, default);
155+
156+
var tcs = new TaskCompletionSource();
157+
158+
Task.Run(() =>
159+
{
160+
try
161+
{
162+
action();
163+
tcs.SetResult();
164+
}
165+
catch (Exception ex)
166+
{
167+
tcs.SetException(ex);
168+
}
169+
finally
170+
{
171+
PInvoke.SetEvent(hEventHandle);
172+
}
173+
});
174+
175+
HANDLE* pEventHandles = stackalloc HANDLE[1];
176+
pEventHandles[0] = hEventHandle;
177+
uint dwIndex = 0u;
178+
179+
PInvoke.CoWaitForMultipleObjects(
180+
(uint)CWMO_FLAGS.CWMO_DEFAULT,
181+
PInvoke.INFINITE,
182+
1u,
183+
pEventHandles,
184+
&dwIndex);
185+
186+
PInvoke.CloseHandle(hEventHandle);
187+
188+
return tcs.Task;
189+
}
146190
}
147191
}

src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ public static extern bool SetEvent(
6969
IntPtr hEvent
7070
);
7171

72-
[DllImport("ole32.dll")]
73-
public static extern uint CoWaitForMultipleObjects(
74-
uint dwFlags,
75-
uint dwMilliseconds,
76-
ulong nHandles,
77-
IntPtr[] pHandles,
78-
out uint dwIndex
79-
);
80-
8172
[DllImport("shell32.dll")]
8273
public static extern IntPtr SHBrowseForFolder(
8374
ref BROWSEINFO lpbi

src/Files.App/Program.cs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Text;
1010
using Windows.ApplicationModel.Activation;
1111
using Windows.Storage;
12-
using static Files.App.Helpers.Win32PInvoke;
1312

1413
namespace Files.App
1514
{
@@ -21,9 +20,6 @@ namespace Files.App
2120
/// </remarks>
2221
internal sealed class Program
2322
{
24-
private const uint CWMO_DEFAULT = 0;
25-
private const uint INFINITE = 0xFFFFFFFF;
26-
2723
public static Semaphore? Pool { get; set; }
2824

2925
static Program()
@@ -250,20 +246,10 @@ private static async void OnActivated(object? sender, AppActivationArguments arg
250246
/// </remarks>
251247
public static void RedirectActivationTo(AppInstance keyInstance, AppActivationArguments args)
252248
{
253-
IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null);
254-
255-
Task.Run(() =>
249+
STATask.RunAsSync(() =>
256250
{
257251
keyInstance.RedirectActivationToAsync(args).AsTask().Wait();
258-
SetEvent(eventHandle);
259252
});
260-
261-
_ = CoWaitForMultipleObjects(
262-
CWMO_DEFAULT,
263-
INFINITE,
264-
1,
265-
[eventHandle],
266-
out uint handleIndex);
267253
}
268254

269255
public static void OpenShellCommandInExplorer(string shellCommand, int pid)
@@ -273,20 +259,10 @@ public static void OpenShellCommandInExplorer(string shellCommand, int pid)
273259

274260
public static void OpenFileFromTile(string filePath)
275261
{
276-
IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null);
277-
278-
Task.Run(() =>
262+
STATask.RunAsSync(() =>
279263
{
280264
LaunchHelper.LaunchAppAsync(filePath, null, null).Wait();
281-
SetEvent(eventHandle);
282265
});
283-
284-
_ = CoWaitForMultipleObjects(
285-
CWMO_DEFAULT,
286-
INFINITE,
287-
1,
288-
[eventHandle],
289-
out uint handleIndex);
290266
}
291267
}
292268
}

0 commit comments

Comments
 (0)