zoukankan      html  css  js  c++  java
  • c# 异步之Func

     public class TestFunc
        {
            private  string Uname = "joyet";
            private  int Num1 = 109;
            private  int Num2 = 5;
            private  int Num3 = 0;
    
            /// <summary>
            /// 测试同步方法
            /// </summary>
            public  void TestYncMethod()
            {
                Console.WriteLine("同步Add方法开始=====");
                Func<int,int,int> func = TestMethods.Add;
                func.Invoke(Num1, Num2);
                Console.WriteLine("同步Add方法结束=====。");
            }
    
            /// <summary>
            /// 测试异步方法
            /// </summary>
            public void TestAsyncPlus()
            {
                Console.WriteLine("异步Add方法开始=====");
                Func<int,int,int> func = TestMethods.Add;
                IAsyncResult result = func.BeginInvoke(Num1, Num2, new AsyncCallback(TestAsyncPlusCallBack), null);
                Console.WriteLine("异步Add方法结束=====。");
            }
    
            /// <summary>
            /// 异步回调用回调函数,当调用结束时会自动调用回调函数,解决了为等待调用结果,而让线程依旧被阻塞的局面。
            /// </summary>
            /// <param name="result"></param>
            private void TestAsyncPlusCallBack(IAsyncResult result)
            {
                Func<int, int, int> f = (Func<int, int, int>)((AsyncResult)result).AsyncDelegate;
                Num3 = f.EndInvoke(result);
                Console.WriteLine("异步Add方法相加结果:" + Convert.ToString(Num3));
            }
        }
  • 相关阅读:
    安卓7.0手机拍照闪退问题解决
    自定义字体TextView
    Android Studio中的CmakeList NDK配置
    动态规划之最长公共子序列(LCS)
    快速排序
    KMP算法实现
    数据结构中的栈
    双向链式线性表(模板实现)
    Android Studio配置OpenCV(非NDK)
    AndroidStudio 1.4配置NDK
  • 原文地址:https://www.cnblogs.com/joyet-john/p/7505134.html
Copyright © 2011-2022 走看看