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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:57:53+00:00 2026-06-08T01:57:53+00:00

this question is a continuation of the first 2 part, anyone who is interested

  • 0

this question is a continuation of the first 2 part, anyone who is interested to see where I come from you can refer to part 1 and part 2, but it is not necessary.

write file need to optimised for heavy traffic

Write file need to optimised for heavy traffic part 2

now i have a working snippet, the relevant part is below:

    public static class memoryStreamClass
    {
        static MemoryStream ms1 = new MemoryStream();

        public static void fillBuffer(string outputString)
        {
            byte[] outputByte = Encoding.ASCII.GetBytes(outputString);

            ms1.Write(outputByte, 0, outputByte.Length);

            if (ms1.Length > 8100)
            {
                emptyBuffer(ms1);
                ms1.SetLength(0);
                ms1.Position = 0;
            }
        }

        static void emptyBuffer(MemoryStream ms)
        {
            FileStream outStream = new FileStream("c:\\output.txt", FileMode.Append);

            ms.WriteTo(outStream);
            outStream.Flush();
            outStream.Close();
        }

the above snippet works fine, and bug free. it output around 8KB of data every write.

now i try to multithread the above code to enhance the performance of an IO write bottleneck and problems appeared. the below snippet is what i tried to attempted.

Basically i have 2 identical memoryStream, if say ms1 is full, it writes ms1 into file and switch to ms2 while ms1 is writing, and vice versa.

    public static class memoryStreamClass
    {
        static MemoryStream ms1 = new MemoryStream();
        static MemoryStream ms2 = new MemoryStream();
        static int c = 1;

        public static void fillBuffer(string outputString)
        {
            byte[] outputByte = Encoding.ASCII.GetBytes(outputString);

            if (c == 1)
            {
                ms1.Write(outputByte, 0, outputByte.Length);

                if (ms1.Length > 8100)
                {
                    c = 2;

                    Thread thread1 = new Thread( () => emptyBuffer(ms1));
                    thread1.Start();

                    ms1.SetLength(0);
                    ms1.Position = 0;
                }
            }
            else
            {
                ms2.Write(outputByte, 0, outputByte.Length);

                if (ms2.Length > 8100)
                {
                    c = 1;

                    Thread thread2 = new Thread(() => emptyBuffer(ms2));
                    thread2.Start();

                    ms2.SetLength(0);
                    ms2.Position = 0;

                }
            }
        }

the above code can compile and run, however, the output write is not always 8KB, and it writes way too frequently (than my single thread program). can someone enlighten my and points out what is wrong with my program? thank you very much

  • 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-08T01:57:55+00:00Added an answer on June 8, 2026 at 1:57 am

    Your code is horribly broken, and your idea of using two buffers to improve performance is almost certainly an over-optimisation. However, there’s one obvious problem in this block of code:

    Thread thread1 = new Thread( () => emptyBuffer(ms1));
    thread1.Start();
    
    ms1.SetLength(0);
    ms1.Position = 0;
    

    What this code does is:

    • start a thread to process a buffer
    • immediately clear that buffer

    The problem is that your “clear” code will almost certainly execute before your thread has a chance to start (because generally speaking, an executing method will complete before the thread context changes). So, by the time you call emptyBuffer, your MemoryStream is already empty.

    Your statics are a bad idea; if you were to pass a non-static instance to the emptyBuffer method, and then set ms1 = new MemoryStream(), you would probably have better functioning code. But ultimately, this code is conceptually flawed and you should look at redesigning.

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

Sidebar

Related Questions

This question is the continuation of I created textarea expander from script but after,
This is a continuation from the previous stackoverflow jQuery question I asked, but I
This is a continuation of this question from yesterday . Here are my three
Ok - this is in continuation from my earlier question about sending an email
A continuation from this question I need a SQL statement that returns the number
Ok this is a continuation from this question: How to make a simple Hello
In a continuation from this question . I'm trying to bind a given function
As a continuation in my thought patterns from this question: Saxon in Java: XSLT
This is an in-depth continuation of my question from earlier this morning , which
This question is continuation from my earlier post titled selecting digits from regular expression.

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.