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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:21:45+00:00 2026-06-04T00:21:45+00:00

I have been wondering… Do the new Async constructs actually allow for intermediate results

  • 0

I have been wondering…

Do the new Async constructs actually allow for intermediate results without writing new code on the consumer side?

Let me elaborate with an example.

Let’s say we have a method that downloads data into a byte array or a stream, whichever is better.

Is it possible, that a method that awaits said method can start consuming the byte array or stream before the download is complete with the built in async features? What I could do is raise events everytime a certain part of the stream is downloaded, but that increases clutter again.

Let me write a code example (from the top of my head, will not be correct code!) that I (would) expect to result in intermediate results, given the awaited method returns such results.

io.Stream data = await downloadData();
byte[4096] buffer = new byte[4096];
while (!data.EOF) {
  (await?) data.Read(buffer, offset, 4096);
}

As of now, Stream.Read is not awaitable, do we have any method of doing this, though? I want to be able to await through a transfer. Am I overseeing anything?

  • 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-04T00:21:47+00:00Added an answer on June 4, 2026 at 12:21 am

    If you’re using Stream in .Net 4.5, you can use its ReadAsync() method for this:

    Stream stream = await downloadData();
    byte[4096] buffer = new byte[4096];
    int read;
    do
    {
        read = await stream.ReadAsync(buffer, 0, buffer.Length);
        // do something with buffer here
    } while (read > 0);
    

    If you mean that your stream does not have a method such as ReadAsync(), but does support asynchrony using the BeginXxx(), EndXxx() pattern, and you want to use await, then you can do that too:

    do
    {
        read = await Task.Factory.FromAsync<byte[], int, int, int>(
            stream.BeginRead, stream.EndRead, buffer, 0, buffer.Length, null);
        // do something with buffer here
    } while (read > 0);
    

    But, if you only have a synchronous Read() method, the only way to use it with async would be to start a new Task on a background thread:

    do
    {
        read = await Task.Run(() => stream.Read(buffer, 0, buffer.Length));
        // do something with buffer here
    } while (read > 0);
    

    The problem with this code is that it still blocks a thread, which is what async is trying to avoid. But it might still be useful if the method runs on a GUI thread, and you can’t run the whole loop on a background thread.

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

Sidebar

Related Questions

I've got a brand new code-signing certificate and have been wondering which files I
I'm new to objective-c and have been wondering what's the point of creating properties?
I have been wondering whether there is any code out there that enables representing
I am quite new to web development, and recently, I have been wondering if
I have been wondering for a while, after asking different people and without any
I have been wondering how much content you should typically allow a CMS to
I have been wondering why the code below is giving me different output on
I have been wondering what is better way of using code. Is there any
I have been wondering about the reload() function in python, which seems like it
I have been wondering about the performance of regular expression implementations lately, and have

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.