Skip to content

Commit c682f2a

Browse files
committed
Init
1 parent a600c6e commit c682f2a

File tree

3 files changed

+33
-85
lines changed

3 files changed

+33
-85
lines changed

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

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -147,57 +147,43 @@ public static Task Run(Func<Task> func)
147147
return tcs.Task;
148148
}
149149

150-
public static Task<T?> RunAsSync<T>(Func<Task<T>> func)
150+
public unsafe static Task RunAsSync(Action action)
151151
{
152-
HANDLE hEventHandle = default;
152+
Debug.Assert(Thread.CurrentThread.GetApartmentState() is ApartmentState.STA);
153153

154-
unsafe
155-
{
156-
hEventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, true, false, default);
157-
}
154+
HANDLE hEventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, true, false, default);
158155

159-
var tcs = new TaskCompletionSource<T?>();
156+
var tcs = new TaskCompletionSource();
160157

161-
Thread thread =
162-
new(async () =>
158+
Task.Run(() =>
159+
{
160+
try
163161
{
164-
PInvoke.OleInitialize();
165-
166-
try
167-
{
168-
tcs.SetResult(await func());
169-
}
170-
catch (Exception ex)
171-
{
172-
tcs.SetException(ex);
173-
}
174-
finally
175-
{
176-
PInvoke.SetEvent(hEventHandle);
177-
PInvoke.OleUninitialize();
178-
}
179-
})
162+
action();
163+
tcs.SetResult();
164+
}
165+
catch (Exception ex)
180166
{
181-
IsBackground = true,
182-
Priority = ThreadPriority.Normal
183-
};
184-
185-
thread.SetApartmentState(ApartmentState.STA);
186-
thread.Start();
187-
188-
unsafe
189-
{
190-
HANDLE* pEventHandles = stackalloc HANDLE[1];
191-
pEventHandles[0] = hEventHandle;
192-
uint dwIndex = 0u;
193-
194-
PInvoke.CoWaitForMultipleObjects(
195-
(uint)CWMO_FLAGS.CWMO_DEFAULT,
196-
PInvoke.INFINITE,
197-
1u,
198-
pEventHandles,
199-
&dwIndex);
200-
}
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);
201187

202188
return tcs.Task;
203189
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,6 @@ public static extern int GetDpiForWindow(
8181
IntPtr hwnd
8282
);
8383

84-
[DllImport("ole32.dll")]
85-
public static extern uint CoWaitForMultipleObjects(
86-
uint dwFlags,
87-
uint dwMilliseconds,
88-
ulong nHandles,
89-
IntPtr[] pHandles,
90-
out uint dwIndex
91-
);
92-
9384
[DllImport("shell32.dll")]
9485
public static extern IntPtr SHBrowseForFolder(
9586
ref BROWSEINFO lpbi
@@ -135,11 +126,6 @@ public static extern uint WaitForMultipleObjectsEx(
135126
bool bAlertable
136127
);
137128

138-
[DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)]
139-
public static extern bool ResetEvent(
140-
IntPtr hEvent
141-
);
142-
143129
[DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)]
144130
public static extern uint WaitForSingleObjectEx(
145131
IntPtr hHandle,

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)