public static MaybeAwaitable <T> Get <T>(object key, Func <T> f) { if (Thread.CurrentThread == MainThread) { return(new MaybeAwaitable <T>(f())); } lock (Queue) { if (!Enqueued.Add(key)) { return((MaybeAwaitable <T>)EnqueuedWaiting[key]); } T result = default(T); Task <T> proxy = new Task <T>(() => result); MaybeAwaitable <T> awaitable = new MaybeAwaitable <T>(proxy.GetAwaiter()); EnqueuedWaiting[key] = awaitable; Queue.Enqueue(() => { result = f != null ? f.Invoke() : default(T); proxy.Start(); lock (Queue) { EnqueuedWaiting.Remove(key); Enqueued.Remove(key); } }); return(awaitable); } }
public static MaybeAwaitable <T> GetForceQueue <T>(Func <T> f) { PreventQueueOverload(); lock (Queue) { MaybeAwaitable <T> awaitable = new MaybeAwaitable <T>(() => true); Queue.Enqueue(() => awaitable.SetResult(f != null ? f.Invoke() : default));
public static MaybeAwaitable <T> Get <T>(Func <T> f) { if (Thread.CurrentThread == MainThread) { return(new MaybeAwaitable <T>(f())); } lock (Queue) { T result = default(T); Task <T> proxy = new Task <T>(() => result); MaybeAwaitable <T> awaitable = new MaybeAwaitable <T>(proxy.GetAwaiter()); Queue.Enqueue(() => { result = f != null ? f.Invoke() : default(T); proxy.Start(); }); return(awaitable); } }