Initial commit: FluidExplorer:从零实现的 WinUI 3 文件资源管理器替代品(Mica、命令栏、面包屑)
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
using Microsoft.UI.Dispatching;
|
||||
|
||||
namespace FluidExplorer.Helpers;
|
||||
|
||||
/// <summary>DispatcherQueue 的 await 化封装:把 UI 线程更新变成可等待的操作,便于批量提交与顺序保证。</summary>
|
||||
public static class DispatcherQueueExtensions
|
||||
{
|
||||
public static Task EnqueueAsync(this DispatcherQueue queue, Action action)
|
||||
{
|
||||
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
if (!queue.TryEnqueue(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
tcs.TrySetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
}))
|
||||
{
|
||||
tcs.TrySetException(new InvalidOperationException("DispatcherQueue 已关闭,无法调度到 UI 线程。"));
|
||||
}
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
public static Task<T> EnqueueAsync<T>(this DispatcherQueue queue, Func<T> func)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
if (!queue.TryEnqueue(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
tcs.TrySetResult(func());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
}))
|
||||
{
|
||||
tcs.TrySetException(new InvalidOperationException("DispatcherQueue 已关闭,无法调度到 UI 线程。"));
|
||||
}
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
/// <summary>即发即忘的 UI 调度(不需要等待结果的场合)。</summary>
|
||||
public static void Post(this DispatcherQueue queue, Action action) => queue.TryEnqueue(() => action());
|
||||
}
|
||||
Reference in New Issue
Block a user