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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:13:04+00:00 2026-06-10T16:13:04+00:00

I have written the following C# function: static string ReadLineCRLF(System.IO.Stream Stream, ref byte[] sharedBuffer,

  • 0

I have written the following C# function:

static string ReadLineCRLF(System.IO.Stream Stream, ref byte[] sharedBuffer, int bufferSize = 1024)
{
    StringBuilder responseString = new StringBuilder("");
    string returnString = "";
    byte[] buffer = new byte[bufferSize];
    bool stopreading = false;
    bool firstexecution = true;
    while (!stopreading)
    {
        int readBytes;
        if (firstexecution && sharedBuffer.Length > 0)
        {
            readBytes = sharedBuffer.Length;
            sharedBuffer.CopyTo(buffer, 0);
        }
        else
        {
            readBytes = Stream.Read(buffer, 0, bufferSize); //BLOCKING HERE
        }
        firstexecution = false;
        if (readBytes > 0)
        {
            int crIndex = Array.IndexOf(buffer, (byte)13); //13 = ASCII value for a carriage return
            if (crIndex > -1 && Array.IndexOf(buffer, (byte)10, crIndex + 1) == crIndex + 1) //10 = ASCII value for line feed
            {

                stopreading = true;
                sharedBuffer = readBytes - crIndex - 2  > 0 ? ArraySlice<byte>(buffer, crIndex+2, readBytes-crIndex-2) : new byte[] { };
                readBytes = crIndex;
            }
            if (readBytes > 0)
            {
                responseString.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, readBytes));
            }
            if (stopreading)
            {
                returnString = responseString.ToString();
            }
        }
        if (!stopreading && readBytes <= 0)
        {
            returnString = null;
            stopreading = true;
            sharedBuffer = new byte[] { };
        }
    }
    return returnString;

}

This function is blocking at readBytes = Stream.Read(buffer, 0, bufferSize); according to my Stack Explorer and using up a lot of computer performance. The only thing this function should do, is reading one line from a stream which is only ended by CRLF (“\r\n”).

According to the MSDN Stream.Read returns less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached. and it should usually block and not use up CPU-performance. The implementation will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file).

So why does it – according to my CLR Stack Explorer – use up so much performance (up to 70%)? I do not see any logical mistake and I think it should wait until some bytes can be received. It also seems that this behaviour does not always occur but one or two days after the start of the application execution on a Windows server.

ADDITIONAL EXPLANATION:
As the bytes are read using chunks, it might be that too many bytes are read and stored in the buffer. Therefore I use a shared buffer, allowing it to use it again to go on reading the next line. Once a line is completely read, I remove it from the buffer.

The ArraySlice function looks like this:

public static T[] ArraySlice<T>(T[] data, int index, int length)
{
    T[] result = new T[length];
    Array.Copy(data, index, result, 0, length);
    return result;
}
  • 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-10T16:13:06+00:00Added an answer on June 10, 2026 at 4:13 pm

    How do you know that Read is blocking, and not just taking a while to do what it needs to?

    IO is expensive; I would expect fully expect that a significant amount of the time spent in this method to be inside of the Read call. the 70% you say sounds about right. You don’t appear to be misusing the method, so the higher the percentage of time spent reading is really meaning a lower amount of overhead doing everything else. Rather than thinking of it as losing 70% of the time doing the read, I look at that as losing 30% of your time doing non-reading activities. Not a bad stat, but also not without room for improvement either.

    Before delving too far though, make sure you’re not in the realm of micro-optimizations. I can tell you now that it doesn’t look like you’re doing it all wrong, so unless you’ve benchmarked your code and determined that it’s running slower than what is acceptable for your requirements, just don’t worry about the performance. If your program isn’t performing fast enough to do it’s job, then you’ll need to start with how long it’s currently taking and how long it needs to take to be “fast enough”.

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

Sidebar

Related Questions

i have written the following code class Program { static void Main(string[] args) {
I have written the following function but it's isn't returning anything when I run
I have written the following algorithm in order to evaluate a function in MatLab
I have written the following function meant to update a student in my database:
So I am working on Problem 31 . I have written the following function
If you look at http://www.danfarrellwright.com/screwsline/front_end/product.php?product_id=104 I have written the following function to add the
I have written the following function to calculate a check digit in R. verhoeffCheck
I have written the following function.. and executed using WinHugs teneven = [x |
In my function for displaying text in textarea,i have written following lines of code
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];

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.