Exemplo n.º 1
0
        public virtual Task <SignalResult> Subscribe(TimeSpan timeout, CancellationToken cancellationToken, params string[] eventKeys)
        {
            var tcs      = new TaskCompletionSource <SignalResult>();
            var timerKey = Guid.NewGuid().ToString();

            var signalAction = new SafeHandleEventAndSetResultAction(SignalBus, tcs, timerKey, eventKeys);

            foreach (var eventKey in eventKeys)
            {
                SignalBus.AddHandler(eventKey, signalAction.Handler);
            }

            if (cancellationToken != CancellationToken.None)
            {
                cancellationToken.Register(signalAction.SetCanceled);
            }

            var timer = new Timer(signalAction.SetTimedOut,
                                  null,
                                  timeout,
                                  timeout);

            _timers.TryAdd(timerKey, timer);

            return(tcs.Task);
        }
Exemplo n.º 2
0
        public virtual Task <SignalResult> Subscribe(TimeSpan timeout, CancellationToken cancellationToken, params string[] eventKeys)
        {
            var tcs = new TaskCompletionSource <SignalResult>();

            var signalAction = new SafeHandleEventAndSetResultAction(SignalBus, tcs, eventKeys, timeout);

            foreach (var eventKey in eventKeys)
            {
                SignalBus.AddHandler(eventKey, signalAction.Handler);
            }

            if (cancellationToken != CancellationToken.None)
            {
                cancellationToken.Register(signalAction.SetCanceled);
            }

            _signalActions.Add(signalAction);

            // Make sure the timer that checks for Subscription timeouts is running
            EnsureTimeoutTimer();

            return(tcs.Task);
        }