Skip to content

Commit fab5674

Browse files
committed
Init
1 parent 7507098 commit fab5674

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
@@ -269,3 +269,8 @@ HCERTSTORE
269269
HCRYPTMSG
270270
CERT_QUERY_ENCODING_TYPE
271271
CertGetNameString
272+
CreateEvent
273+
SetEvent
274+
CoWaitForMultipleObjects
275+
CWMO_FLAGS
276+
INFINITE

src/Files.App.Storage/Windows/Managers/STATask.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
using Microsoft.Extensions.Logging;
55
using Windows.Win32;
6+
using Windows.Win32.Foundation;
7+
using Windows.Win32.System.Com;
8+
using Windows.Win32.Security;
69

710
namespace Files.App.Storage
811
{
@@ -140,5 +143,46 @@ public static Task Run(Func<Task> func, ILogger? logger = null)
140143

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

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

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

71-
[DllImport("ole32.dll")]
72-
public static extern uint CoWaitForMultipleObjects(
73-
uint dwFlags,
74-
uint dwMilliseconds,
75-
ulong nHandles,
76-
IntPtr[] pHandles,
77-
out uint dwIndex
78-
);
79-
8071
[DllImport("shell32.dll")]
8172
public static extern IntPtr SHBrowseForFolder(
8273
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)