示例#1
0
        public bool TryRegisterNew(object taskCompletionSource, out TriggerReference triggerReference)
        {
            if (taskCompletionSource == null)
            {
                throw new ArgumentNullException(nameof(taskCompletionSource));
            }
            if (!TaskCompletionSourceAccessor.IsTaskCompletionSource(taskCompletionSource))
            {
                throw new ArgumentException($"Input object must be a TaskCompletionSource`1, but got {taskCompletionSource.GetType()}", nameof(taskCompletionSource));
            }

            var task = TaskCompletionSourceAccessor.GetTask(taskCompletionSource);

            triggerReference = task.AsyncState as TriggerReference;
            if (triggerReference != null)
            {
                return(false);
            }

            triggerReference = new TriggerReference
            {
                Id = _numericIdGenerator.NewId()
            };
            task.SetAsyncState(triggerReference);

            return(true);
        }
示例#2
0
        public bool Monitor(object taskCompletionSource)
        {
            if (taskCompletionSource == null)
            {
                throw new ArgumentNullException(nameof(taskCompletionSource));
            }
            if (!TaskCompletionSourceAccessor.IsTaskCompletionSource(taskCompletionSource))
            {
                throw new ArgumentException($"Input object must be a TaskCompletionSource`1, but got {taskCompletionSource.GetType()}", nameof(taskCompletionSource));
            }

            var task = TaskCompletionSourceAccessor.GetTask(taskCompletionSource);

            if (!(task.AsyncState is TriggerReference triggerReference))
            {
                return(false);
            }

#warning Check if already subscribed to the completion.
            task.ContinueWith(OnTaskComplete, TaskContinuationOptions.ExecuteSynchronously);

            return(true);
        }