Advertisement
Guest User

Untitled

a guest
Jul 25th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Reactive.Concurrency;
  4. using System.Reactive.Linq;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main ()
  11.         {
  12.             var q = new BlockingCollection<int>();
  13.             var r = new Random();
  14.             int id = 1;
  15.  
  16.             using (Observable
  17.                 .Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1), new EventLoopScheduler())
  18.                 .Subscribe(x => {
  19.                     var v = q.Take();
  20.                     /*int v;
  21.                     if (q.TryTake(out v))*/
  22.                     Console.WriteLine("{0}: {1} ({2})", DateTime.Now, v, q.Count);
  23.                 }))
  24.             using (Observable
  25.                 .Timer(TimeSpan.Zero, TimeSpan.FromSeconds(3), new EventLoopScheduler())
  26.                 .Subscribe(x => {
  27.                     int n = r.Next(0, 5);
  28.                     for (int i = 0; i < n; ++i)
  29.                         q.Add(id++);
  30.                 })) {
  31.                 Console.ReadLine();
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement