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

  • Home
  • SEARCH
  • 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 6588273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:01:39+00:00 2026-05-25T17:01:39+00:00

Trying to figure out whether or not I should use async methods or not

  • 0

Trying to figure out whether or not I should use async methods or not such as:

  • TcpListener.BeginAcceptTcpClient
  • TcpListener.EndcceptTcpClient

and

  • NetworkStream.BeginRead
  • NetworkStream.EndRead

as opposed to their synchronous TcpListener.AcceptTcpClient and NetworkStream.Read versions. I’ve been looking at related threads but I’m still a bit unsure about one thing:

Question: The main advantage of using an asynchronous method is that the GUI is not locked up. However, these methods will be called on separate Task threads as it is so there is no threat of that. Also, TcpListener.AcceptTcpClient blocks the thread until a connection is made so there is no wasted CPU cycles. Since this is the case, then why do so many always recommend using the async versions? It seems like in this case the synchronous versions would be superior?

Also, another disadvantage of using asynchronous methods is the increased complexity and constant casting of objects. For example, having to do this:

private void SomeMethod()
{
    // ...

    listener.BeginAcceptTcpClient(OnAcceptConnection, listener);
}

private void OnAcceptConnection(IAsyncResult asyn)
{
    TcpListener listener = (TcpListener)asyn.AsyncState;

    TcpClient client = listener.EndAcceptTcpClient(asyn);
}

As opposed to this:

TcpClient client = listener.AcceptTcpClient();

Also it seems like the async versions would have much more overhead due to having to create another thread. (Basically, every connection would have a thread and then when reading that thread would also have another thread. Threadception!)

Also, there is the boxing and unboxing of the TcpListener and the overhead associated with creating, managing, and closing these additional threads.

Basically, where normally there would just be individual threads for handling individual client connections, now there is that and then an additional thread for each type of operation performed (reading/writing stream data and listening for new connections on the server’s end)

Please correct me if I am wrong. I am still new to threading and I’m trying to understand this all. However, in this case it seems like using the normal synchronous methods and just blocking the thread would be the optimal solution?

  • 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-25T17:01:39+00:00Added an answer on May 25, 2026 at 5:01 pm

    TcpListener.AcceptTcpClient blocks the thread until a connection is made so there is no wasted CPU cycles.

    But there is also no work getting done. A Thread is a very expensive operating system object, about the most expensive there is. Your program is consuming a megabyte of memory without it being used while the thread blocks on connection request.

    However, these methods will be called on separate Task threads as it is so there is no threat of that

    A Task is not a good solution either, it uses a threadpool thread but the thread will block. The threadpool manager tries to keep the number of running TP threads equal to the number of cpu cores on the machine. That won’t work well when a TP thread blocks for a long time. It prevents other useful work from being done by other TP threads that are waiting to get their turn.

    BeginAcceptTcpClient() uses a so-called I/O completion callback. No system resources are consumed while the socket is listening. As soon as a connection request comes in, the operating system runs an APC (asynchronous procedure call) which grabs a threadpool thread to make the callback. The thread itself is in use for, typically, a few microseconds. Very efficient.

    This kind of code will get a lot simpler in the next version of C# with the next async and await keywords. End of the year, maybe.

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

Sidebar

Related Questions

I am trying to figure out whether or not the current date falls within
I'm trying to figure out whether or not a user likes our brand page.
I'm currently trying to figure out when to use ViewModels and when not to.
I'm trying to figure out the best practices for deciding whether or not to
I’m trying to figure out whether FormsAuthentication.RedirectFromLoginPage (called inside method M() ), performs redirection
In the course of trying to figure out whether it's possible to contain text
I'm working on a little something and I am trying to figure out whether
I'm a Java newbie. I'm trying to figure out whether a number is a
I'm trying to figure out the best way to determine whether I'm in the
Trying to figure out which to use.

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.