예제 #1
0
        public AsyncBuffer(int boundedCapacity, TimeSpan sample)
        {
            var buffer = new BroadcastBlock <Action>(n => n, new DataflowBlockOptions()
            {
                BoundedCapacity = boundedCapacity
            });

            _StartProcessDisposable = _StartProcessSubject
                                      .Subscribe(a => buffer.SendAsync(a)
                                                 , exc => Error.OnNext(exc));
            var b = buffer
                    .AsObservable();

            if (sample != TimeSpan.Zero)
            {
                b = b.Sample(sample);
            }
            _bufferDisposable = b
                                .SubscribeOn(NewThreadScheduler.Default)
                                .Subscribe(a => {
                try {
                    a();
                    Pushed.OnNext(_lastContext);
                } catch (Exception exc) {
                    Error.OnNext(exc);
                }
            }, exc => Error.OnNext(exc));
        }
예제 #2
0
        private void streamCallback(Stream stream)
        {
            Semaphore semaphore             = new Semaphore(0, 1);
            Action <Lazy <byte[]> > handler = imageData =>
            {
                try
                {
                    if (imageData.Value != null)
                    {
                        var value = imageData.Value;
                        writeHeader(stream, value.Length);
                        stream.Write(value, 0, value.Length);
                        writeFooter(stream);
                        stream.Flush();
                    }
                }
                catch (HttpListenerException) { }
                catch (ProtocolViolationException)
                {
                    semaphore.Release();
                }
            };
            var unsubscriber = currentImageEncoded.AsObservable().Subscribe(new BroadcastObserver <Lazy <byte[]> >(semaphore, handler));

            semaphore.WaitOne();
            unsubscriber.Dispose();
            stream.Close();
        }
        public static async Task Test()
        {
            bb
            .AsObservable()
            .Subscribe(i =>
                       Interlocked.Add(ref sum, i));
            bb
            .AsObservable()
            .Subscribe(i =>
                       Interlocked.Add(ref sum, i));

            bb.Post(1);
            await Task.Delay(100);

            bb.Complete();
            await bb.Completion;
        }
예제 #4
0
        public static IObservable <T> InitBufferedObservable <T>(this ISubject <T> subject, Action <Exception> onError)
        {
            BroadcastBlock <T> buffer = new BroadcastBlock <T>(n => n);

            subject.ObserveOn(TaskPoolScheduler.Default)
            .Subscribe(a => buffer.SendAsync(a), onError);
            return(buffer.AsObservable());
        }
예제 #5
0
파일: LibTest.cs 프로젝트: lulzzz/HedgeHog
        public void Observe_Buffer2()
        {
            var bb = new BroadcastBlock <string>(s => { /*Debug.WriteLine("b:" + s);*/ return(s); });

            bb.AsObservable().Buffer(1.FromSeconds()).Where(s => s.Any()).Subscribe(s => {
                //        Debug.WriteLine(DateTime.Now.ToString("mm:ss.fff") + Environment.NewLine + string.Join("\t" + Environment.NewLine, s));
                Debug.WriteLine(DateTime.Now.ToString("mm:ss.fff") + Environment.NewLine + "\t" + s.Count);
            });
            for (int i = 0; i < 1000000; i++)
            {
                bb.SendAsync(DateTime.Now.ToString(i + " mm:ss.fff"));
            }
            Thread.Sleep(10000);
        }
예제 #6
0
 public static IDisposable SubscribeToYieldingObservable <T>(this BroadcastBlock <T> bb, Action <T> action, Action <T, Exception> error = null)
 {
     return(bb.AsObservable().Subscribe(t => {
         try {
             action(t);
         } catch (Exception exc) {
             if (error != null)
             {
                 error(t, exc);
             }
             else
             {
                 GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(new LogMessage(exc));
             }
         }
     }));
 }
        public void Start(DataStreamRegistry registry)
        {
            if (registry == null)
            {
                throw new ArgumentNullException(nameof(registry));
            }

            _block = new BroadcastBlock <IEnumerable <ConfigurationEntry> >(i => i);
            registry.Register("configuration", _block.AsObservable());

            _block.Post(GetValues());

            _subscription = ChangeToken.OnChange(() => _configuration.GetReloadToken(), () =>
            {
                _block.Post(GetValues());
            });
        }
예제 #8
0
        public void RX_Semple()
        {
            var bb = new BroadcastBlock <int>(i => i);
            var d  = bb.AsObservable()
                     //.Where(i => i % 3 == 0)
                     .Subscribe(i => {
                Debug.WriteLine("bb:" + i);
                Thread.Sleep(1000);
            });
            var isDone = false;

            Observable.Range(0, 100)
            .ObserveOn(ThreadPoolScheduler.Instance)
            .Subscribe(i => {
                bb.Post(i);
                Thread.Sleep(110);
            }, () => isDone = true);
            while (!isDone)
            {
            }
            Debug.WriteLine("Waiting for ab.");
            Thread.Sleep(2000);
        }