Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:20:13+00:00 2026-06-13T14:20:13+00:00

I’m using the latest Rx2 from NuGet. In example code below, the subscription is

  • 0

I’m using the latest Rx2 from NuGet. In example code below, the subscription is processed on a new thread and doesn’t block the generation of new values. That’s all good. However, why are the values generated by the observable processed on the new thread sequentially?

static void Main(string[] args)
{
  printThreadId("Started");
  var observable = Observable.Generate<int, int>(
      0,
      x => {
        printThreadId("Comparing " + x);
        return x < 5; },
      x => {
        printThreadId("Incrementing " + x);
        return x + 1; },
      x => {
        printThreadId("Selecting " + x);
        return x; },
      NewThreadScheduler.Default
  );

  var disp = observable
    .ObserveOn(NewThreadScheduler.Default)
    .Subscribe(state =>
    {
      printThreadId("Processing " + state);
      Thread.Sleep(1000);
    });

  Console.ReadLine();
  disp.Dispose();
}

static Action<string> printThreadId = (prefix) => Console.WriteLine("{0} on [Worker.{1}]", prefix, Thread.CurrentThread.ManagedThreadId);

The above code takes around 5 seconds to complete. I expected each action to be executed on a new thread and this is not happening.

Code below schedules a new task and the whole process completes in a little over 1 second.

var disp = observable
 .ObserveOn(NewThreadScheduler.Default)
 .Subscribe(state =>
 {
   printThreadId("Processing " + state);
   NewThreadScheduler.Default.Schedule(() =>
   {
      printThreadId("Long running task " + state);
      Thread.Sleep(1000);
   });
 });

Is there a better way to schedule long running tasks to run concurrently using ObserveOn and Subscribe?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:20:15+00:00Added an answer on June 13, 2026 at 2:20 pm

    The reactive framework specifically ensures that subsequent value are serialized with no overlap. This helps stop many concurrency issues. This behaviour is by design.

    The NewThreadScheduler queues up actions, so if there are actions scheduled back-to-back the scheduler doesn’t bother with creating a new thread – it just uses the currect thread. When there is a gap (something might be queued but it is for the future) then the current thread is allowed to end and a new one created when the action is due.

    If you want the subscriptions to run in parallel you have to go out of your way to make it happen. But the good news is that it’s not too hard.

    If you keep your existing observable then you can try this:

    Func<int, IObservable<Unit>> process = state =>
        Observable.Start(() =>
        {
            printThreadId("Processing " + state);
            Thread.Sleep(1000);
            return Unit.Default;
        });
    
    var query =
        from x in observable
        select process(x);
    
    var disp =
        query
            .Merge()
            .ObserveOn(NewThreadScheduler.Default)
            .Subscribe();
    

    I got this result when I ran it:

    Started on [Worker.23]
    Comparing 0 on [Worker.26]
    Selecting 0 on [Worker.26]
    Processing 0 on [Worker.20]
    Incrementing 0 on [Worker.26]
    Comparing 1 on [Worker.26]
    Selecting 1 on [Worker.26]
    Incrementing 1 on [Worker.26]
    Comparing 2 on [Worker.26]
    Selecting 2 on [Worker.26]
    Processing 1 on [Worker.18]
    Incrementing 2 on [Worker.26]
    Comparing 3 on [Worker.26]
    Selecting 3 on [Worker.26]
    Processing 3 on [Worker.13]
    Incrementing 3 on [Worker.26]
    Comparing 4 on [Worker.26]
    Selecting 4 on [Worker.26]
    Processing 4 on [Worker.12]
    Incrementing 4 on [Worker.26]
    Comparing 5 on [Worker.26]
    Processing 2 on [Worker.8]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.