using Sandbox; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace SeamlessClient.Utilities { public static class AsyncInvoke { public static Task InvokeAsync(Action action, [CallerMemberName] string caller = "SeamlessClient") { //Jimm thank you. This is the best var ctx = new TaskCompletionSource(); MySandboxGame.Static.Invoke(() => { try { action.Invoke(); ctx.SetResult(null); ctx.Task.ContinueWith(task => task.Dispose()); } catch (Exception e) { ctx.SetException(e); } }, caller); return ctx.Task; } public static Task InvokeAsync(Func action, [CallerMemberName] string caller = "SeamlessClient") { //Jimm thank you. This is the best var ctx = new TaskCompletionSource(); MySandboxGame.Static.Invoke(() => { try { ctx.SetResult(action.Invoke()); ctx.Task.ContinueWith(task => task.Dispose()); } catch (Exception e) { ctx.SetException(e); } }, caller); return ctx.Task; } public static Task InvokeAsync(Func action, T1 arg, [CallerMemberName] string caller = "SeamlessClient") { //Jimm thank you. This is the best var ctx = new TaskCompletionSource(); MySandboxGame.Static.Invoke(() => { try { ctx.SetResult(action.Invoke(arg)); ctx.Task.ContinueWith(task => task.Dispose()); } catch (Exception e) { ctx.SetException(e); } }, caller); return ctx.Task; } public static Task InvokeAsync(Func action, T1 arg, T2 arg2, [CallerMemberName] string caller = "SeamlessClient") { //Jimm thank you. This is the best var ctx = new TaskCompletionSource(); MySandboxGame.Static.Invoke(() => { try { ctx.SetResult(action.Invoke(arg, arg2)); ctx.Task.ContinueWith(task => task.Dispose()); } catch (Exception e) { ctx.SetException(e); } }, caller); return ctx.Task; } public static Task InvokeAsync(Func action, T1 arg, T2 arg2, T3 arg3, [CallerMemberName] string caller = "SeamlessClient") { //Jimm thank you. This is the best var ctx = new TaskCompletionSource(); MySandboxGame.Static.Invoke(() => { try { ctx.SetResult(action.Invoke(arg, arg2, arg3)); ctx.Task.ContinueWith(task => task.Dispose()); } catch (Exception e) { ctx.SetException(e); } }, caller); return ctx.Task; } } }