locked
Subscribe is synchronous? RRS feed

  • Question

  • Trying to follow the Hands on Labs for Rx. According to the pdf, 

     

      static void Main(string[] args)
      {
       IObservable<int> source = Observable.Range(1, 50000);
    
       var subscription = source.Subscribe(
        x =>
        {
         Console.WriteLine("OnNext: {0}.", x);
        },
        x => Console.WriteLine("OnError: {0}.", x.Message),
        () => Console.WriteLine("OnCompleted."));
     
       Console.WriteLine("Press Enter to unsubscribe");
       Console.ReadLine();   
    subscription.Dispose(); }
    should show the Press enter to unsubscribe first, and then the output for each item in Range(...). Using the official 1.0 release, this doesn't seem to be the case. All 50000 lines are getting output and then the "Press enter to unsubscribe" line's being printed. I tried moving subscription.Dispose() before the readline expecting none or at most few lines getting printed. Still, it appears that all 50K lines get printed followed by the "Press enter to unsubscribe line". Am I missing something, or is Subscribe now actually synchronous? If that's the case, is there any real documentation for the v1 release?

     


    Saturday, August 6, 2011 10:29 AM

Answers

  • Release documentation is available here: http://msdn.microsoft.com/en-us/library/hh242985(v=VS.103).aspx

    The Hands-On labs were created a few months ago but I think they're worthy of going through.

    Up there, you might want to pass a scheduler like so:

    Observable.Range(1, 50000, Scheduler.TaskPool);
    

    That might fix your problem, also leaving the subscription.Dispose() after the ReadLine is the correct behavior as you want to unsubscribe after pressing enter, not after printing Press Enter to Unsubscribe.

    • Marked as answer by hsft Saturday, August 6, 2011 1:26 PM
    Saturday, August 6, 2011 11:58 AM

All replies

  • Release documentation is available here: http://msdn.microsoft.com/en-us/library/hh242985(v=VS.103).aspx

    The Hands-On labs were created a few months ago but I think they're worthy of going through.

    Up there, you might want to pass a scheduler like so:

    Observable.Range(1, 50000, Scheduler.TaskPool);
    

    That might fix your problem, also leaving the subscription.Dispose() after the ReadLine is the correct behavior as you want to unsubscribe after pressing enter, not after printing Press Enter to Unsubscribe.

    • Marked as answer by hsft Saturday, August 6, 2011 1:26 PM
    Saturday, August 6, 2011 11:58 AM
  • Thanks...adding the scheduler works in making it async. The documentation is really lacking though - an updated HoL would be great. It's not really official production quality without official production documentation, tutorials and training materials.
    Saturday, August 6, 2011 1:26 PM