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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:47:55+00:00 2026-05-25T23:47:55+00:00

I have very similar code when using the standard BeginRead and EndRead methods from

  • 0

I have very similar code when using the standard BeginRead and EndRead methods from the TcpClient and using Task.Factory.FromAsync.

Here are some examples.. Error handling code not shown.

Task.Factory.FromAsync:

private void Read(State state)
{
    Task<int> read = Task<int>.Factory.FromAsync(state.Stream.BeginRead, state.Stream.EndRead, state.Bytes, state.BytesRead, state.Bytes.Length - state.BytesRead, state, TaskCreationOptions.AttachedToParent);

    read.ContinueWith(FinishRead);
}

private void FinishRead(Task<int> read)
{
    State state = (State)read.AsyncState;

    state.BytesRead += read.Result;
}

Standard use of callbacks with BeginRead and EndRead:

private void Read(State state)
{
    client.BeginRead(state.Bytes, state.BytesRead, state.Bytes.Length - state.Bytes.Read, FinishRead, state);
}

private void FinishRead(IAsyncResult async)
{
    State state = (State)async.AsyncState;

    state.BytesRead += state.Stream.EndRead(async);
}

Both of these work fine but I am curious of their differences. The lines of code for both are pretty much equivalent and they both seem to perform the exact same function and have the same efficiency. Which one is preferable? What would you rather see in production code?

  • 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-05-25T23:47:55+00:00Added an answer on May 25, 2026 at 11:47 pm

    I would much rather see Task<T> based code:

    • It provides for composition more easily; for example, it’s reasonably easy to write a method which takes a collection of Task<T> tasks and returns another task which represents the majority verdict of those tasks. Likewise you can wait until any one of a collection of tasks has completed, etc.
    • It provides more flexible scheduling of where the continuation is run.
    • It allows the task itself to be returned with type safety and a lot more information than the somewhat anaemic IAsyncResult type returned by BeginRead.
    • It’s simpler to specify error handling and cancellation with tasks than using the Begin/End model.
    • Task<T> is getting better language support in C# 5 with async/await – if your codebase already uses Task<T> pervasively, it’ll be much easier to take advantage of this

    Basically in modern code running on .NET 4, Task<T> is the idiomatic way of representing an on-going task. It’s a much richer environment to work in than earlier attempts, and I would embrace it if you have the chance. Obviously if you’re using .NET 3.5 or earlier, life is a bit harder, but I’m assuming that as you’re asking the question, Task<T> is an option…

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

Sidebar

Related Questions

I have a piece of code VERY similar to this one http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#JavaMailFetching I the
I have 8 different XAML DataTemplates that are all very similar. Here are 2
I am using a dataset with variables that have very similar names. I have
I have code very similar to this example three times in a code behind.
I've been using some code very similar to the following to populate a gridview
I have a very similar setup to the person here: PHP Background Processes i.e
Okay I have seen some very similar questions here but none seem to be
I am using the JanRain library, and implementing code very similar to their server
I have a very similar situation to the person who asked: Can I serve
Scenario: Let's say I have four very similar applications (i.e. most of the functionality

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.