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