Exemple #1
0
        public void ObservableProgressor()
        {
            var source = Progressor <int> .CreateFromArray(new[] { 0, 1, 2, 3, 4, 5 });

            var progressor = Progressor <int> .CreateFromIObservable(source);

            source.Consume();
            var indexA = 0;
            var indexB = 0;

            using
            (
                progressor.SubscribeAction
                (
                    value =>
            {
                Assert.AreEqual(value, indexB);
                indexB++;
            }
                )
            )
            {
                while (progressor.TryTake(out var item))
                {
                    Assert.AreEqual(item, indexA);
                    indexA++;
                }
                Assert.AreEqual(6, indexA);
                Assert.AreEqual(indexA, indexB);
            }
        }
Exemple #2
0
        public void ThreadedUseArray()
        {
            var source = Progressor <int> .CreateFromArray(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

            using (var handle = new ManualResetEvent(false))
            {
                int[] count = { 0, 0, 0 };
                var   work  = new WaitCallback
                              (
                    _ =>
                {
                    Interlocked.Increment(ref count[0]);
                    handle.WaitOne();
                    foreach (var item in source)
                    {
                        GC.KeepAlive(item);
                        Interlocked.Increment(ref count[2]);
                    }
                    Interlocked.Increment(ref count[1]);
                }
                              );
                ThreadPool.QueueUserWorkItem(work);
                ThreadPool.QueueUserWorkItem(work);
                while (Volatile.Read(ref count[0]) != 2)
                {
                    Thread.Sleep(1);
                }
                handle.Set();
                while (Volatile.Read(ref count[1]) != 2)
                {
                    Thread.Sleep(1);
                }
                Assert.AreEqual(10, Volatile.Read(ref count[2]));
            }
        }
Exemple #3
0
 public override IEnumerable <T> GetDynamicPartitions()
 {
     if (_source is T[] array)
     {
         return(Progressor <T> .CreateFromArray(array));
     }
     if (_source is IList <T> list)
     {
         return(Progressor <T> .CreateFromIList(list));
     }
     return(Progressor <T> .CreateFromIEnumerable(_source));
 }