MyTask

/*
    public static string HelloWorld()
    {
        return "Hello World!"; 
    }

    static void Main(string[] args)
    {
        var task = BeginTask(HelloWorld); // non-blocking call

        string result = task(); // block and wait

    }
     * **/
    public class MyTask
    {
        public delegate R AsyncTask<R>();

        public static AsyncTask<R> BeginTask<R>(AsyncTask<R> function)
        {
            R retv = default(R);
            bool completed = false;

            object sync = new object();

            IAsyncResult asyncResult = function.BeginInvoke(
                    iAsyncResult =>
                    {
                        lock (sync)
                        {
                            completed = true;
                            retv = function.EndInvoke(iAsyncResult);
                            Monitor.Pulse(sync);
                        }
                    }, null);

            return delegate
            {
                lock (sync)
                {
                    if (!completed)
                    {
                        Monitor.Wait(sync);
                    }
                    return retv;
                }
            };
        }
    }
原文地址:https://www.cnblogs.com/gxivwshjj/p/6690950.html