Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.4 kB
1
Indexable
Never
using System;
using System.ComponentModel.Design;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var t = MethodAsync();
            t.Wait();
            Console.ReadKey();
        }

        private static async Task MethodAsync()
        {
            Console.WriteLine("Hello world");
            string s = await new MyAwaitable();
            Console.WriteLine(s);
            Console.WriteLine("Bye world");
        }
    }

    public class MyAwaitable
    {
        public MyAwaiter GetAwaiter()
        {
            return new MyAwaiter();
        }
    }

    public class MyAwaiter : INotifyCompletion
    {
        private Action m_continuation;
        private bool m_completed;
        private Task m_task;
        public MyAwaiter()
        {
            m_completed = false;
            m_task = Task.Delay(2000).ContinueWith(t => m_completed = true);
        }

        public void OnCompleted(Action continuation)
        {
            m_task.ContinueWith(t => continuation());
            m_continuation = continuation;
        }

        public string GetResult()
        {
            return "hey";
        }

        public bool IsCompleted => m_completed;
    }
}