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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:44:03+00:00 2026-06-16T21:44:03+00:00

I am trying to port some TcpClient dependant code to .net 4.5, using StreamSocket

  • 0

I am trying to port some TcpClient dependant code to .net 4.5, using StreamSocket and DataReader instead.

I have a function named ReadLine() which is used everywhere. By using DataReader in the body (LoadAsync()) of this code, my method is forced to be marked with the async keyword.

The chain reaction is as follows:
Now I have hundreds of places where I have to add async to calling methods and apply await to underlying async method calls.

This leads me to my next question…
Is there an easy way to wrap ReadLine() so that calling methods are not aware that it’s an asynchronous method, so that I don’t have to change the rest of my code?

Also…
I often use this method call in a loop, from multiple places. If these methods are now marked async, i’m afraid I might be reading data off a stream when I shouldn’t be, which will cause all sorts of nightmares. Is it a problem or am I thinking too far ahead?

  • 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-16T21:44:04+00:00Added an answer on June 16, 2026 at 9:44 pm

    Also… I often use this method call in a loop, from multiple places. If these methods are now marked async, i’m afraid I might be reading data off a stream when I shouldn’t be, which will cause all sorts of nightmares. Is it a problem or am I thinking too far ahead?

    If you always use await whenver you call *Async methods, then your async methods will act just like synchronous methods (except they won’t block). So using await in a loop will work just like you expect.


    async does indeed “grow” through the code base. I usually think of this as similar to the old story about “turtles all the way down”; others have called it a “zombie virus”.

    I describe the deadlock situation in detail on my blog. As I state there, the best option is to allow async to grow.

    If you must create a synchronous wrapper for an asynchronous method, see Stephen Toub’s advice. You can use Task.Result, but you need to do two things:

    • Use ConfigureAwait(false) everywhere. This will sidestep the deadlock situation.
    • Be aware that Result has different error handling semantics.

    For your particular example, something like this should suffice:

    private async Task<string> ReadLineAsync()
    {
      ... // *Every* await in this method and every method it calls
          // must make use of ConfigureAwait(false).
    }
    
    public string ReadLine()
    {
      try
      {
        return ReadLineAsync().Result;
      }
      catch (AggregateException ex)
      {
        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
        throw;
      }
    }
    

    Please carefully consider the complexity before choosing a mixed synchronous/asynchronous code base. It’s not as easy as it first appears.

    P.S. Generally speaking, TCP/IP code should all be asynchronous anyway. It’s usually a good idea to have a continuous asynchronous read going on a socket.

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

Sidebar

Related Questions

I'm trying to port some ASP.Net code to mono. I am testing it using
I'm trying to port some of my c++ code into c. I have the
I am trying to port some code using DevExpress XtraGrid to Wpf. What is
I am trying to port some Python code to .NET, and I was wondering
Currently I am trying to port some VB.NET code to C#. The struct looks
I'm trying to port some code into .net which has a load of #define'd
I'm trying to port some windows code that uses HidD_GetInputReport to linux using libusb.
I am trying to port some c++ code to my android application using NDK.
I'm trying to port some OpenGL 3.2 code from Windows to OS/X 10.8 (using
I am trying to port some code using VC++'s try-except statement to MinGW: bool

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.