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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:15:33+00:00 2026-05-23T10:15:33+00:00

I’m about 15 minutes into my first play with the async CTP … (nice).

  • 0

I’m about 15 minutes into my first play with the async CTP… (nice).

Here’s a really simple server I’ve knocked together:

internal class Server
{
    private HttpListener listener;
    public Server()
    {
        listener = new HttpListener();
        listener.Prefixes.Add("http://*:80/asynctest/");
        listener.Start();
        Go();
    }

    async void Go()
    {
        HttpListenerContext context = await listener.GetContextAsync();
        Go();
        using (var httpListenerResponse = context.Response) 
        using (var outputStream = httpListenerResponse.OutputStream) 
        using (var sw = new StreamWriter(outputStream))
        {
            await sw.WriteAsync("hello world");
        }
    }
}

As can be seen, the async method Go calls itself. In classic non-async world, this would cause a stack overflow. I assume that this isn’t the case with an async method, but I’d like to be sure, one way or the other. Anyone?

  • 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-23T10:15:33+00:00Added an answer on May 23, 2026 at 10:15 am

    Let’s break it down into something simpler:

    async static void Go()
    {
        await Something();
        Go();
        await SomethingElse();
    }
    

    How does the compiler deal with this?

    Basically this becomes something like this sketch:

    class HelperClass
    {
        private State state = STARTSTATE;
        public void DoIt()
        {
    
            if (state == STARTSTATE) goto START;
            if (state == AFTERSOMETHINGSTATE) goto AFTERSOMETHING;
            if (state == AFTERSOMETHINGELSESTATE) goto AFTERSOMETHINGELSE;
    
            START:
            {
               state = AFTERSOMETHINGSTATE;
               var awaiter = Something().MakeAnAwaiter();
               awaiter.WhenDoneDo(DoIt);
               return;
            }
    
            AFTERSOMETHING:
            {
               Go();
               state = AFTERSOMETHINGELSESTATE;
               var awaiter = SomethingElse().MakeAnAwaiter();
               awaiter.WhenDoneDo(DoIt);
               return;
            }
    
            AFTERSOMETHINGELSE:
    
            return;
        }
    
        static void Go()
        {
            var helper = new HelperClass();
            helper.DoIt();
        }
    

    Now all you have to remember is that when each asynchronous operation completes, “DoIt” is scheduled to be called again by the message loop (on the appropriate instance of the helper of course).

    So what happens? Work it out. You call Go for the first time. That makes helper number one and calls DoIt. That calls Something(), gets a task back, makes an awaiter for that task, tells the awaiter “when you’re done, call helper1.DoIt” and returns.

    A tenth of a second later the task completes and the message loop calls helper1’s DoIt. helper1’s state is AFTERSOMETHINGSTATE, so we take the goto and call Go. That makes helper2 and calls DoIt on that. That calls Something(), gets a task back, makes an awaiter for that task, tells the awaiter “when you’re done, call DoIt on helper2” and returns control back to helper1’s DoIt. That calls SomethingElse, makes an awaiter for that task, and tells it “when you’re done doing something else, call helper1’s DoIt”. It then returns.

    Now we have two tasks outstanding and no code on the stack. One of the tasks will complete first. Suppose the SomethingElse task completes first. The message loop calls helper1.DoIt(), which immediately returns. Helper1 is now garbage.

    Later the message loop calls helper2.DoIt(), and branches to AFTERSOMETHING. Now Go() is called, which creates helper3…

    So no, there’s no unbounded recursion here. Every time Go executes it runs as far as asynchronously starting Something() and then it returns to its caller. The call to the stuff after “something” happens later. “Go” is only ever on the stack once at a time.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.