title: Reactive Extensions (Rx) and asynchronous class
body: <p>I've read in this post: "<a href="http://themechanicalbride.blogspot.com/2009/07/developing-with-rx-part-2-converting.html" rel="nofollow noreferrer">The joy of Rx: The event-based asynchronous pattern vs IObservable</a>" that the use of EBAP is discourage. What is the best way to design an asynchronous component with the new Rx extensions (something like the PrimeNumberCalculator example of msdn)? </p>
  
  <p>Thank you in advance.</p>
  
  <p><strong>Update</strong>
  I managed to write my own prime number calculator, I would like to hear your opinions:</p>
  
  <pre><code>using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading;
  
  namespace ConsoleApplication13
  {
      public class PrimeNumberCalculator
      {
          private readonly Subject&lt;int&gt; primeSubject;
          private IDisposable currentSubscription;
  
          public PrimeNumberCalculator()
          {
              primeSubject = new Subject&lt;int&gt;();
              Primes = primeSubject.Hide();
          }
  
          public IObservable&lt;int&gt; Primes{ get; private set; }
  
          /// &lt;summary&gt;
          /// Determine if n is prime.
          /// &lt;/summary&gt;
          private static bool IsPrime(ArrayList primes, int n, out int firstDivisor)
          {
              bool foundDivisor = false;
              bool exceedsSquareRoot = false;
  
              int i = 0;
              firstDivisor = 1;
  
              // Stop the search if:
              // there are no more primes in the list,
              // there is a divisor of n in the list, or
              // there is a prime that is larger than 
              // the square root of n.
              while ( (i &lt; primes.Count) &amp;&amp; !foundDivisor &amp;&amp; !exceedsSquareRoot)
              {
                  // The divisor variable will be the smallest 
                  // prime number not yet tried.
                  int divisor = (int)primes[i++];
  
                  // Determine whether the divisor is greater
                  // than the square root of n.
                  if (divisor * divisor &gt; n)
                  {
                      exceedsSquareRoot = true;
                  }
                  // Determine whether the divisor is a factor of n.
                  else if (n % divisor == 0)
                  {
                      firstDivisor = divisor;
                      foundDivisor = true;
                  }
              }
  
              return !foundDivisor;
          }
  
          /// &lt;summary&gt;
          /// Itereates from 1 to numberToTest and returns all primes.
          /// &lt;/summary&gt;
          private IEnumerable&lt;int&gt; PrimeNumberIterator(int numberToTest)
          {
              var primes = new ArrayList();
              var n = 5;
  
              // Add the first prime numbers.
              primes.Add(2);
              primes.Add(3);
  
              // Do the work.
              while (n &lt; numberToTest)
              {
                  int firstDivisor;
                  if (IsPrime(primes, n, out firstDivisor))
                  {
                      // Report to the client that a prime was found.
                      yield return n;
  
                      Thread.Sleep(5000); //simulate long running task.
                      primes.Add(n);
  
                  }
                  // Skip even numbers.
                  n += 2;
              }
          }
  
          /// &lt;summary&gt;
          /// Begin a prime number exploration.
          /// If there is some exploration in progress unsubscribe.
          /// &lt;/summary&gt;
          public void IsPrime(int numberToTest)
          {
              if (currentSubscription != null) currentSubscription.Dispose();
              currentSubscription = PrimeNumberIterator(numberToTest)
                                          .ToObservable()
                                          .Subscribe(primeSubject.OnNext);
          }
  
          /// &lt;summary&gt;
          /// Cancel a prime number exploration
          /// &lt;/summary&gt;
          public void Cancel()
          {
              if (currentSubscription != null) currentSubscription.Dispose();
          }
      }
  
      internal class Program
      {
  
  
          private static void Main(string[] args)
          {
              var primeNumberCalculator = new PrimeNumberCalculator();
              primeNumberCalculator.Primes.Subscribe(p =&gt; Console.WriteLine("Is prime {0}", p));
  
              var exit = false;
              do
              {
                  Console.WriteLine("Write a number to explore and press enter: ");
                  var input = Console.ReadLine();
                  int primeToExplore;
                  if(int.TryParse(input, out primeToExplore))
                  {
                      primeNumberCalculator.IsPrime(primeToExplore);
                  }
                  else {
                      primeNumberCalculator.Cancel();
                      exit = true;
                  }
              } while (!exit);
          }
      }
  }
  </code></pre>