예제 #1
0
        private CallbackResult <TResponse> OnWait <TResponse>(string messageId)
        {
            var spinner   = new SpinWait();
            var startWait = DateTime.UtcNow;

            while (true)
            {
                // Cancel
                if (_cts.IsCancellationRequested)
                {
                    return(CallbackResult <TResponse> .Error("Cancelled"));
                }

                // Timeout
                var now  = DateTime.UtcNow;
                var diff = now.Subtract(startWait);
                if (diff > _timeout)
                {
                    return(CallbackResult <TResponse> .Error("Timeout"));
                }

                // Prevent CPU burning
                spinner.SpinOnce();

                if (_callbackMessages.TryGetValue(messageId, out var value))
                {
                    _callbackMessages.TryRemove(messageId, out _);

                    var convertedValue = _valueConverterService.Convert <TResponse>(value);
                    return(new CallbackResult <TResponse>(convertedValue));
                }
            }
        }