Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

AsyncVoidMethodBuilder.AwaitOnCompleted - how this is invoked when awaiter.iscompleted is false

$
0
0

HI

Let's say we have a custom awaitable and awaiter implemented. If I set IsCompleted property of awaiter to false and then never set it to true, then too OnCompleted method of awaiter is called. How AsyncVoidMethodBuilder.AwaitOnCompleted dispatches call to awaiter.Oncompleted method , if I am not initiating any task in my custom awaiter. Please look at the code below. This code never returns, my question is If my custom awaiter doesn't initiates any task , nor it reset's Iscompleted property to true then how OnCompleted method is invoked. My understandig is, once awaiter finishes its task then only Oncompleted method is invoked by AsyncMethodBuilder. So why even Oncompleted method is invoked and how this is invoked ?

    class Program
    {
        static void Main(string[] args)
        {

            Task task = DummyAsync();
            task.Wait();
        }

        static async Task DummyAsync()
        {
            TestAwaitable rob = new TestAwaitable();
            string text = await rob;
            Console.WriteLine("I got {0}", text);
            Console.WriteLine(new StackTrace());
        }
    }

    internal class TestAwaitable
    {
        internal TestWaiter GetAwaiter()
        {
            return new TestWaiter();
        }
    }

    internal class TestWaiter : INotifyCompletion
    {
        internal bool IsCompleted { get { return false; } }

        public void OnCompleted(Action continuation)
        {
            Console.WriteLine("This will be called");
             Console.WriteLine(continuation.Method.DeclaringType);
        }

        public string GetResult()
        {
            return "Surprise!";
        }
    }


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>