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 8012895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:28:35+00:00 2026-06-04T19:28:35+00:00

I am educating myself on Parallel.Invoke, and parallel processing in general, for use in

  • 0

I am educating myself on Parallel.Invoke, and parallel processing in general, for use in current project. I need a push in the right direction to understand how you can dynamically\intelligently allocate more parallel ‘threads’ as required.

As an example. Say you are parsing large log files. This involves reading from file, some sort of parsing of the returned lines and finally writing to a database.

So to me this is a typical problem that can benefit from parallel processing.

As a simple first pass the following code implements this.

Parallel.Invoke(
  ()=> readFileLinesToBuffer(),
  ()=> parseFileLinesFromBuffer(),
  ()=> updateResultsToDatabase()    
);

Behind the scenes

  1. readFileLinesToBuffer() reads each line and stores to a buffer.
  2. parseFileLinesFromBuffer comes along and consumes lines from buffer and then let’s say it put them on another buffer so that updateResultsToDatabase() can come along and consume this buffer.

So the code shown assumes that each of the three steps uses the same amount of time\resources but lets say the parseFileLinesFromBuffer() is a long running process so instead of running just one of these methods you want to run two in parallel.

How can you have the code intelligently decide to do this based on any bottlenecks it might perceive?

Conceptually I can see how some approach of monitoring the buffer sizes might work, spawning a new ‘thread’ to consume the buffer at an increased rate for example…but I figure this type of issue has been considered in putting together the TPL library.

Some sample code would be great but I really just need a clue as to what concepts I should investigate next. It looks like maybe the System.Threading.Tasks.TaskScheduler holds the key?

  • 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-04T19:28:36+00:00Added an answer on June 4, 2026 at 7:28 pm

    Have you tried the Reactive Extensions?

    http://msdn.microsoft.com/en-us/data/gg577609.aspx

    The Rx is a new tecnology from Microsoft, the focus as stated in the official site:

    The Reactive Extensions (Rx)… …is a library to compose
    asynchronous and event-based programs using observable collections and
    LINQ-style query operators.

    You can download it as a Nuget package

    https://nuget.org/packages/Rx-Main/1.0.11226

    Since I am currently learning Rx I wanted to take this example and just write code for it, the code I ended up it is not actually executed in parallel, but it is completely asynchronous, and guarantees the source lines are executed in order.

    Perhaps this is not the best implementation, but like I said I am learning Rx, (thread-safe should be a good improvement)

    This is a DTO that I am using to return data from the background threads

    class MyItem
    {
        public string Line { get; set; }
        public int CurrentThread { get; set; }
    }
    

    These are the basic methods doing the real work, I am simulating the time with a simple Thread.Sleep and I am returning the thread used to execute each method Thread.CurrentThread.ManagedThreadId. Note the timer of the ProcessLine it is 4 sec, it’s the most time-consuming operation

    private IEnumerable<MyItem> ReadLinesFromFile(string fileName)
    {
        var source = from e in Enumerable.Range(1, 10)
                     let v = e.ToString()
                     select v;
    
        foreach (var item in source)
        {
            Thread.Sleep(1000);
            yield return new MyItem { CurrentThread = Thread.CurrentThread.ManagedThreadId, Line = item };
        }
    }
    
    private MyItem UpdateResultToDatabase(string processedLine)
    {
        Thread.Sleep(700);
        return new MyItem { Line = "s" + processedLine, CurrentThread = Thread.CurrentThread.ManagedThreadId };
    }
    
    private MyItem ProcessLine(string line)
    {
        Thread.Sleep(4000);
        return new MyItem { Line = "p" + line, CurrentThread = Thread.CurrentThread.ManagedThreadId };
    }
    

    The following method I am using it just to update the UI

    private void DisplayResults(MyItem myItem, Color color, string message)
    {
        this.listView1.Items.Add(
            new ListViewItem(
                new[]
                {
                    message, 
                    myItem.Line ,
                    myItem.CurrentThread.ToString(), 
                    Thread.CurrentThread.ManagedThreadId.ToString()
                }
            )
            {
                ForeColor = color
            }
        );
    }
    

    And finally this is the method that calls the Rx API

    private void PlayWithRx()
    {
        // we init the observavble with the lines read from the file
        var source = this.ReadLinesFromFile("some file").ToObservable(Scheduler.TaskPool);
    
        source.ObserveOn(this).Subscribe(x =>
        {
            // for each line read, we update the UI
            this.DisplayResults(x, Color.Red, "Read");
    
            // for each line read, we subscribe the line to the ProcessLine method
            var process = Observable.Start(() => this.ProcessLine(x.Line), Scheduler.TaskPool)
                .ObserveOn(this).Subscribe(c =>
                {
                    // for each line processed, we update the UI
                    this.DisplayResults(c, Color.Blue, "Processed");
    
                    // for each line processed we subscribe to the final process the UpdateResultToDatabase method
                    // finally, we update the UI when the line processed has been saved to the database
                    var persist = Observable.Start(() => this.UpdateResultToDatabase(c.Line), Scheduler.TaskPool)
                        .ObserveOn(this).Subscribe(z => this.DisplayResults(z, Color.Black, "Saved"));
                });
        });
    }
    

    This process runs totally in the background, this is the output generated:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been educating myself. Reading this : The engine evaluates each rule from right
I'm engaged in educating myself about C# via Troelsen's Pro C# book. I'm familiar
Hi i am educating myself oop principles. I would like to know if this
My team is working on educating some of our developers about testing. They understand
I'm driving myself crazy trying to understand Expressions in LINQ. Any help is much
For my own education I am curious what compilers use which C++ front-end and
I am in need of education about browsers and how they send up dates
I have just started using and educating my self of Java spring. I now
I have set myself upon a journey to educate my coworkers (all have accepted
I'm emulating a 4 bit microprocessor. I need to keep track of the registers,

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.