Waiting up to firstItemTimeout for the first item to become available, then return all other items that are immediately available. - CSharp System

CSharp examples for System:DateTime

Description

Waiting up to firstItemTimeout for the first item to become available, then return all other items that are immediately available.

Demo Code


using System.Threading;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System;/*from  w  w w  .  ja  va 2  s  .c o  m*/

public class Main{
        /// <summary>
        /// Waiting up to <paramref name="firstItemTimeout">firstItemTimeout</paramref>
        /// for the first item to become available, then return all other items that
        /// are immediately available.
        /// </summary>
        public static void TakeAvailable<T>(this BlockingCollection<T> collection, IList<T> destination, TimeSpan firstItemTimeout)
        {
            T item;
            var timeout = firstItemTimeout;
            while (collection.TryTake(out item, timeout))
            {
                destination.Add(item);
                timeout = TimeSpan.Zero;
            }
        }
}

Related Tutorials