コード例 #1
0
        public static async Task <T> RunOnUIThreadAsync <T>(this Func <Task <T> > func)
        {
            var tcs = new TaskCompletionSource <T>();

            System.Action action = async() =>
            {
                try
                {
                    var result = await func();

                    tcs.SetResult(result);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            };
            await action.OnUIThreadAsync();

            return(await tcs.Task);
        }
コード例 #2
0
        public static async Task RunOnUIThreadAsync(this Func <Task> func)
        {
            var tcs = new TaskCompletionSource <bool>();

            System.Action action = async() =>
            {
                try
                {
                    await func();

                    tcs.SetResult(true);
                }
                catch (Exception ex)
                {
                    tcs.SetException(ex);
                }
            };
            await action.OnUIThreadAsync();

            await tcs.Task;
        }