Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        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));
Exemplo n.º 3
0
        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);
            }
        }