zoukankan      html  css  js  c++  java
  • 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;
                    }
                };
            }
        }
  • 相关阅读:
    20188477 编程作业
    原型设计
    案例分析
    编程作业
    阅读任务
    准备工作
    原型设计作业
    案例分析作业
    编程作业
    阅读任务
  • 原文地址:https://www.cnblogs.com/gxivwshjj/p/6690950.html
Copyright © 2011-2022 走看看