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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:39:47+00:00 2026-06-05T05:39:47+00:00

An application that I’m building reads logs from some source and displays it on

  • 0

An application that I’m building reads logs from some source and displays it on a grid. The logs can be a few MB to a few GB in size. To prevent any issues with memory, I’m using a grid and paging through the logs 500 lines at a time. This is what I hope to do:

I want to create a thread which will read logs and write them to a file about 500 lines each time, then signal another thread that logs have been written. The other thread will then read the file and display the lines on the grid and signal the first thread that it has finished reading. this goes on till there are no more logs to be written to the file.

Is it possible to switch between threads like this?

  • 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-05T05:39:48+00:00Added an answer on June 5, 2026 at 5:39 am

    Yes, of course, it’s a variation of the producer-consumer model.

    You can use some basic building blocks here like Thread and AutoResetEvent. The “producer” reads lines from the logs and posts them to a file (maybe you can use an in-memory buffer instead?) and then signals the other thread to read them:

    AutoResetEvent consumerEvent = new AutoResetEvent(false);
    AutoResetEvent producerEvent = new AutoResetEvent(false);
    
    // producer code
    while(/* lines still available */)
    {
        // read 500 lines
        // write to shared file
        consumerEvent.Set(); // signal consumer thread
        producerEvent.WaitOne(); // wait to be signaled to continue
    }
    

    And the consumer code:

    while(/* termination not received */)
    {
        consumerEvent.WaitOne(); // wait for the producer to finish     
        // read lines from file and put them in the grid
        producerEvent.Set(); // allow producer to read more logs
    }
    

    This will allow some degree of parallelism between the consumer reading the file and the producer reading more logs and preparing the next batch.

    When the producer has finished with the logs it can put a special termination message in the file to signal the consumer to exit gracefully.

    This is one strategy and it’s pretty low-level and error-prone. You can skip the shared file entirely and use an in-memory buffer in the form of a BlockingCollection.

    Define a ProducerTask class to hold some lines of text:

    class ProducerTask 
    {
        public String[] Lines { get; set; }
    }
    

    This task will hold 500 lines at a time.

    Then use Task and BlockingCollection (.NET 4.0+) as follows:

    BlockingCollection<ProducerTask> buffer = new BlockingCollection<ProducerTask>(1);
    
    // producer task
    while(/* lines still available */)
    {
        // read 500 lines
        ProducerTask p = new ProducerTask();
        buffer.Add(p); // this will block until the consumer takes the previous task
    }
    
    // consumer task
    while(/* termination not received */)
    {
        ProducerTask p = buffer.Take(); // blocks if no task is available
        // put the lines in the grid
    }
    

    Much more simple and elegant.

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

Sidebar

Related Questions

I found that some application, like GoodReader or Docs to Go, once installed can
I'm writing an application that loads some javascript into a WebBrowser object. To prevent
Say that I've got a web application that can store Persons in a database.
I'm building an application that needs to open self-signed HTTPS SSL URLs in java.
I have a .NET application that uses an assembly (.dll) that defines some method:
I'm building an application that stores lots of data per user (possibly in gigabytes).
An application that uses Flask framework, can start the built-in werkzeug development server simply
I'm writing an application that needs to perform some tasks only if there is
I have this small application that I'm building as an exercise to learn the
I have an application that enabled that user to switch language at run-time from

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.