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); }
public IValueContainer Decompose(object value) { return(new TaskCompletionSourceContainer { Task = TaskCompletionSourceAccessor.GetTask(value) }); }
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())); }
public void ActivateTrigger(long id, TaskResult value) { object taskCompletionSource; lock (_triggers) { taskCompletionSource = _triggers[id]; } var task = TaskCompletionSourceAccessor.GetTask(taskCompletionSource); task.TrySetResult(value); }
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); }