internal void Release(IDelay delay)
 {
     if (Interlocked.Decrement(ref _RefCount) == 0)
     {
         delay.Wait(Bucket.LimitRequestZone.RequestRate.TimePerRequest).ContinueWith((_) =>
         {
             if (Interlocked.CompareExchange(ref _RefCount, -1, 0) == 0)
             {
                 Bucket.Close();
             }
         }, TaskContinuationOptions.ExecuteSynchronously);
     }
 }
Exemplo n.º 2
0
        private void Run()
        {
            while (_status)
            {
                List <T> items = Poll();

                if (items.Count == 0)
                {
                    _delay.Wait();
                }
                else
                {
                    _callback(items);
                    _delay.Reset();
                }
            }
        }
Exemplo n.º 3
0
        private Task MainLoop(CancellationToken token)
        {
            return(Task.Factory.StartNew(async() =>
            {
                while (!token.IsCancellationRequested)
                {
                    var currentLayers = layers;

                    while (currentLayers.Length == 0)
                    {
                        await lightClient.SetColors(
                            lightClient.Lights.Select(l => RGB.Black),
                            token);

                        WaitHandle.WaitAny(new[]
                        {
                            layersExistEvent,
                            token.WaitHandle
                        });

                        if (token.IsCancellationRequested)
                        {
                            return;
                        }

                        currentLayers = layers;
                    }

                    var colors = currentLayers
                                 .Select(l => l.Colors)
                                 .Aggregate((colors1, colors2) =>
                    {
                        return colors1.Zip(colors2, (rgb1, rgb2) => rgb1 + rgb2).ToArray();
                    });

                    await Task.WhenAll(new[]
                    {
                        lightClient.SetColors(colors, token),
                        delayProvider.Wait(delay, token)
                    });
                }
                ;
            }, TaskCreationOptions.LongRunning));
        }
Exemplo n.º 4
0
        public async Task Transition(ITransition transition, CancellationToken token = default)
        {
            var totalMilliseconds = transition.TotalLength.TotalMilliseconds;

            var stopwatch = stopwatchBuilder.StartNew();

            long elapsed = 0;

            while (!token.IsCancellationRequested && (elapsed = stopwatch.ElapsedMilliseconds) < totalMilliseconds)
            {
                Colors = GetColors(transition, elapsed);

                var delayMs = Math.Min(msPerTransition, totalMilliseconds - elapsed);

                await delay.Wait(TimeSpan.FromMilliseconds(delayMs), token);
            }

            if (!token.IsCancellationRequested)
            {
                Colors = GetColors(transition, elapsed);
            }
        }
            public async Task Run(CancellationToken cancellationToken)
            {
                await DelayImplementation.Wait(Delay, cancellationToken);

                await Action(cancellationToken);
            }
Exemplo n.º 6
0
        private async Task Wait()
        {
            await _delay.Wait(_secondsPerRequest);

            Interlocked.Decrement(ref _UsedSlots);
        }