예제 #1
0
        public static ReadableChannel <bool> Timeout(TimeSpan interval)
        {
            var channel = Nonblocking <bool>(1);

            ChannelsExtensions.Go(async() => {
                await Task.Delay(interval).ConfigureAwait(false);
                //            Console.WriteLine("Time signalling");
                await channel.WriteAsync(true).ConfigureAwait(false);
            });

            return(channel);
        }
예제 #2
0
        public static ReadableChannel <bool> Timer(TimeSpan interval, TimeSpan randomize = default(TimeSpan))
        {
            var channel = Blocking <bool>();

            // TODO: Timer must be disposable to prevent task leaks.
            ChannelsExtensions.Go(async() => {
                var rand = new Random(channel.GetHashCode());
                while (true)
                {
                    var delay = TimeSpan.FromMilliseconds(interval.TotalMilliseconds + randomize.TotalMilliseconds * rand.NextDouble());
                    await Task.Delay(delay).ConfigureAwait(false);
                    await channel.WriteAsync(true).ConfigureAwait(false);
                }
            }).Forget();

            return(channel);
        }
예제 #3
0
        public async Task <T> ReadAsync(CancellationToken cancellationToken, Func <T, bool> acceptanceTest)
        {
            bool disconnected = false;
            T    result       = default(T);

            await new Select {
                ChannelsExtensions.Case(disconnectedChannel, () => {
                    disconnected = true;
                }),
                ChannelsExtensions.Case(dataChannel, data => {
                    result = data;
                }, acceptanceTest)
            }.WaitAsync(cancellationToken);

            if (disconnected)
            {
                throw new TError();
            }
            return(result);
        }