Files
Common-VS2019/SacsMobileMaui/Maui/Utils/SamThreadUtil.cs
T

59 lines
1.7 KiB
C#

using System;
using System.Threading.Tasks;
namespace Sarand.Net.Mobile.Maui.Utils
{
//=======================================================================
public static class SamThreadUtil
{
//-------------------------------------------------------------------
// Thread utilities.
//-------------------------------------------------------------------
/// <summary>Is the current thread the main thread.</summary>
public static bool IsMainThread()
{
return MainThread.IsMainThread;
}
// --- Fire-and-forget dispatch ---
/// <summary>Invoke an action on the main thread without waiting for completion.</summary>
public static void PostToMainThread(Action taskFn)
{
MainThread.BeginInvokeOnMainThread(() =>
{
taskFn();
});
}
// --- Awaitable dispatch ---
/// <summary>Invoke an action on the main thread and await its completion.</summary>
public static Task InvokeOnMainThreadAsync(Action taskFn)
{
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
/// <summary>Invoke an asynchronous function on the main thread and await its completion.</summary>
public static Task InvokeOnMainThreadAsync(Func<Task> taskFn)
{
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
/// <summary>Invoke a function on the main thread and await its result.</summary>
public static Task<T> InvokeOnMainThreadAsync<T>(Func<T> taskFn)
{
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
/// <summary>Invoke an asynchronous function on the main thread and await its result.</summary>
public static Task<T> InvokeOnMainThreadAsync<T>(Func<Task<T>> taskFn)
{
return MainThread.InvokeOnMainThreadAsync(taskFn);
}
// --- The End ---
}
}