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

  • Home
  • SEARCH
  • 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 8149109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:46:57+00:00 2026-06-06T14:46:57+00:00

I am new to the reactive extensions and I would like to use it

  • 0

I am new to the reactive extensions and I would like to use it (in c#) to read a file which contains several streams that are interleaved. Basically the file is in the format ABCDABCDABCD.... I would prefer to read the file sequentially and separate the streams (ie AAA.., BBB.., etc) and process each stream in parallel, using separate threads for each stream.

There will have to be some form of buffering to make sure each stream can remain busy as much as possible (within limits of course). Not all streams start at the same time necessarily, in which case a number of elements have to be skipped for the delayed streams. In this case the buffering might bridge the gap.

The elements in the file are small (4 bytes) so it is quite chatty. Therefore, I’m also looking for a way to deal with this efficiently.

I started out by creating an enumerable to read the file. This could be made to supply a struct which contains the stream ID, or the streams could be separated based on the order (element number modulo number of streams). The later is probably more efficient though.

  • 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-06T14:46:58+00:00Added an answer on June 6, 2026 at 2:46 pm

    This question has ‘it depends’ stamped all over it, especially when you’re talking about performance and efficiency but have provided an example that is somewhat contrived. Namely, your example file is dead simple compared to the real file. However, I will attempt to provide some advice on the off chance that it is useful.

    Here’s a method to turn a stream into an Enumerable<char>. The stream will apply the buffering, this will send one result back at a time. This could be made more efficient (to send back chunks of data), but at some point you need to process them one at a time and it may as well be here. Don’t prematurely optimise.

    IEnumerable<char> ReadBytes(Stream stream)
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            while (!reader.EndOfStream)
                yield return (char)reader.Read();
        }
    }
    

    Now, let’s say this is the processing code for the ‘output’ observables. First, I set the output observables up, and then I subscribe to them as appropriate. Note that I’m using an array here so my output observable index is the array index. One could use a dictionary also, if the stream index couldn’t be turned into a zero-based index.

    var outputs = Enumerable.Repeat(0, 3).Select(_ => new Subject<char>()).ToArray();                                                                                                     
    
    outputs[0].Delay(TimeSpan.FromSeconds(2)).Subscribe(x => Console.WriteLine("hi: {0}", x));
    outputs[1].Delay(TimeSpan.FromSeconds(1)).Subscribe(x => Console.WriteLine("ho: {0}", x));
    outputs[2].Subscribe(x => Console.WriteLine("he: {0}", x));
    

    Notice the use of Subject<char> to send my elements out on. This depends on the type of your element, but char works in the example given. Notice also that I delay the elements only to prove everything is working. They are now independent streams and you can do whatever you want with them.

    OK, given a file stream:

    var file = @"C:\test.txt";
    var buffer = 32;
    var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, buffer);
    

    I can now subscribe and use the modulo index to send to the right output stream:

    ReadBytes(stream)
    .ToObservable(Scheduler.ThreadPool)
    .Select((x,i) => new { Key = (i % 3), Value = x }) // you can change it up here
    .Subscribe(x => outputs[x.Key].OnNext(x.Value));
    

    There are potentially more efficient methods here depending on exactly how you can calculate the target stream, but the idea remains the same.

    Input file contains just one line: ABCABCABCABCABCABC

    Output from running the program is:

    he: C
    he: C
    he: C
    he: C
    he: C
    he: C
    

    One second later:

    ho: B
    ho: B
    ho: B
    ho: B
    ho: B
    ho: B
    

    And then another second:

    hi: A
    hi: A
    hi: A
    hi: A
    hi: A
    hi: A
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to put Reactive Extensions for .NET (Rx) to good use and would
I'm currently working on Windows Phone applications and I would like to use Reactive
This is an academic exercise, I'm new to Reactive Extensions and trying to get
I am quite new to work with Reactive Extensions, so this might be a
This is a question about how to use Reactive Extensions (Rx) in a specific
I'm using reactive extensions in my wp7 app that I'm making and I wish
I'm new to Reactive Extensions for .NET and while playing with it I thought
I'm attempting to use Reactive Extensions (Rx) to subscribe to WebClient.DownloadProgressChanged . As far
I am slowly learning how to use Reactive Extensions for .NET with WPF. There
I want to use reactive-banana to write a traveller game that people can write

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.