Newer
Older
Qwilight / Qwilight / System / HandlingUISystem.cs
@Taehui Taehui on 16 Nov 1 KB 2023-11-16 오후 12:28
using System.Windows.Threading;

namespace Qwilight
{
    public sealed class HandlingUISystem
    {
        public static readonly HandlingUISystem Instance = new();

        public Dispatcher UIHandler { get; set; }

        public void Init(Action<Exception> onUnhandledFault)
        {
            UIHandler = Dispatcher.CurrentDispatcher;
            UIHandler.UnhandledException += (sender, e) =>
            {
                e.Handled = true;
                onUnhandledFault(e.Exception);
            };
        }

        public void HandleParallel(Action onHandle)
        {
            if (UIHandler.CheckAccess())
            {
                onHandle();
            }
            else
            {
                UIHandler.InvokeAsync(onHandle);
            }
        }

        public T Handle<T>(Func<T> onHandle)
        {
            if (UIHandler.CheckAccess())
            {
                return onHandle();
            }
            else
            {
                return UIHandler.Invoke(onHandle);
            }
        }
    }
}