public SynchronousCompletionAsyncResult <object> DoFindAsync(string name)
        {
            var src = new SynchronousCompletionAsyncResultSource <object>();

            void ParentSearch()
            {
                var parentSearch = _parentScope.FindAsync(name);

                if (parentSearch.IsCompleted)
                {
                    src.SetResult(parentSearch.GetResult());
                }
                else
                {
                    parentSearch.OnCompleted(() => src.SetResult(parentSearch.GetResult()));
                }
            }

            if (!_inner.IsCompleted)
            {
                // Guaranteed to be incomplete at this point
                var innerSearch = _inner.FindAsync(name);
                innerSearch.OnCompleted(() =>
                {
                    var value = innerSearch.GetResult();
                    if (value != null)
                    {
                        src.SetResult(value);
                    }
                    else
                    {
                        ParentSearch();
                    }
                });
            }
            else
            {
                ParentSearch();
            }

            return(src.AsyncResult);
        }
Esempio n. 2
0
        public SynchronousCompletionAsyncResult <object> FindAsync(string name)
        {
            var found = Find(name);

            if (found != null)
            {
                return(new SynchronousCompletionAsyncResult <object>(found));
            }
            if (IsCompleted)
            {
                return(new SynchronousCompletionAsyncResult <object>((object)null));
            }
            if (!_pendingSearches.TryGetValue(name, out var tcs))
            {
                // We are intentionally running continuations synchronously here
                _pendingSearches[name] = tcs = new SynchronousCompletionAsyncResultSource <object>();
            }

            return(tcs.AsyncResult);
        }