泛型委托+异步回调+超时机制

写法一:(泛型委托)

       Func<string, object> albumFunC = new Func<string, object>(GetCacheObject);//泛型委托

       object result =albumFunC("参数值");

 

写法二: (泛型委托+超时机制)

        Func<string, object> albumFunC = new Func<string, object>(GetCacheObject);/

        IAsyncResult ar= albumFunC.BeginInvoke("参数值", null, null);//此时CLR会打开一个新的线程去异步执行委托的方法   

        //以下语句为等待返回结果的超时机制

        object result=null;
        ar.AsyncWaitHandle.WaitOne(1000, false);//1秒超时
        if (ar.IsCompleted)
        {
              result = albumFunC.EndInvoke(ar);
        }
        return result;

 

写法三:(泛型委托+异步回调)

       Func< string, object > albumFunC = new Func<string,object>(GetAlbumStyle);
       IAsyncResult ar = albumFunC.BeginInvoke("参数值",  new AsyncCallback(CallBackAlbumMethod), albumFunC);//异步回调

       void onComplete(IAsyncResult ar)
       {
              Func<string, object> andl = (Func<string, object>)ar.AsyncState;
              object result=andl.EndInvoke(ar);
       }
  

 

你可能感兴趣的:(泛型)