/// <summary>Adds a action to perform when a channel can be read</summary>
        /// <param name="ch">The channel to try to read from</param>
        /// <param name="action">the synchronous action to perform with the value that was read</param>
        public Select OnReceive <T>(Channel <T> ch, Action <T> action)
        {
            var @case = new ReceiveCase <T>(ch, action);

            cases.Add(@case);
            quickCases = null;
            return(this);
        }
Exemple #2
0
        /// <summary>Tries to reads from one (and only one) of the added channels.  Does no action if the <paramref name="timeout"/> has been reached</summary>
        /// <returns>True if an action was performed, False if no action was performed and the timeout was reached</returns>
        public async Task <bool> ExecuteAsync(TimeSpan timeout)
        {
            if (timeout == System.Threading.Timeout.InfiniteTimeSpan || timeout == TimeSpan.MaxValue)
            {
                await ExecuteAsync();

                return(true);
            }

            var timedOut       = false;
            var receiveTimeout = new ReceiveCase <DateTime>(Timeout.After(timeout), _ => timedOut = true);
            var idx            = cases.Count;

            cases.Add(receiveTimeout);
            await ExecuteAsync();

            cases.RemoveAt(idx);
            return(!timedOut);
        }