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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:15:12+00:00 2026-06-04T21:15:12+00:00

In C# with async ctp or the vs.net 2011 beta we can write recursive

  • 0

In C# with async ctp or the vs.net 2011 beta we can write recursive code like this:

public async void AwaitSocket()
{
    var socket = await this.AcceptSocketAsync(); //await socket and >>return<< to caller
    AwaitSocket(); //recurse, note that the stack will never be deeper than 1 step since await returns..
    Handle(socket); // this will get called since "await" returns
}

In this specific sample, the code async waits for a tcp socket and once it has been accepted, it will recurse and async wait for another one.

This seems to work fine, since the await section will make the code return to the caller and thus, not cause a stack overflow.

So two questions here:

  1. if we ignore the fact we are dealing with sockets in this sample.
    Is it OK to do stack free recursion this way? or are there drawbacks Im missing?

  2. from an IO perspective, would the above code be enough to handle all incoming requests?
    I mean by just waiting for one, and once it is accepted start waiting for another one.
    Will some requests fail because of this somehow?

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

    From the discussion above, I guess something like this will be the best approach.
    Please give feedback

    public async void StartAcceptingSockets()
    {
        await Task.Yield(); 
        // return to caller so caller can start up other processes/agents
        // TaskEx.Yield in async ctp , Task.Yield in .net 4.5 beta
    
        while(true)
        {
            var socket = await this.AcceptSocketAsync();
            HandleAsync(socket); 
            //make handle call await Task.Yield to ensure the next socket is accepted as fast 
            //as possible and dont wait for the first socket to be completely handled
        } 
    }
    
    private async void HandleAsync(Socket socket)
    {
          await Task.Yield(); // return to caller
    
          ... consume the socket here...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Async Tracking code in Google Analytics looks like this: var _gaq = _gaq
@Override @Async public void asyncExceptionTest() { int i=1/0; } How can I log this
I have been playing round with the Async CTP this morning and have a
The following code will not compile against the Async CTP in Visual Studio 2010:
This example fails: static async void Main(string[] args) { try { await TaskEx.Run(() =>
I am playing with the new Async CTP bits, and I can't get it
What is the difference between Task.WaitAll() and Task.WhenAll() from the Async CTP? Can you
Using the async CTP from Microsoft for .NET, is it possible to catch an
I installed the Async CTP w/o knowing it would cause problems with Asp.NET MVC
I am studying the new Async CTP and going through some sample code, I

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.