Exemplo n.º 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);
        }
Exemplo n.º 2
0
 public IValueContainer Decompose(object value)
 {
     return(new TaskCompletionSourceContainer
     {
         Task = TaskCompletionSourceAccessor.GetTask(value)
     });
 }
Exemplo n.º 3
0
        public void SubscribeToTrigger(long id, Action <TaskResult> action)
        {
            object taskCompletionSource;

            lock (_triggers)
            {
                taskCompletionSource = _triggers[id];
            }
            var task = TaskCompletionSourceAccessor.GetTask(taskCompletionSource);

            task.ContinueWith(t => action(t.ToTaskResult()));
        }
Exemplo n.º 4
0
        public void ActivateTrigger(long id, TaskResult value)
        {
            object taskCompletionSource;

            lock (_triggers)
            {
                taskCompletionSource = _triggers[id];
            }
            var task = TaskCompletionSourceAccessor.GetTask(taskCompletionSource);

            task.TrySetResult(value);
        }
Exemplo n.º 5
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);
        }