• .NET 同步 异步 委托


    1、定义委托:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    
    namespace DelegateWebApp
    {
    
        /// <summary>
        /// 定义委托
        /// </summary>
        /// <param name="i"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public  delegate int SumDelegate(int i,int y);
    
        public class CounterService
        {
            
            public int Sum(int i,int y)
            {
                return i + y;
            }
    
    
            /// <summary>
            /// 异步等待
            /// </summary>
            /// <param name="a"></param>
            /// <param name="b"></param>
            /// <returns></returns>
            public int WaitSum(int a, int b)
            {
    
                for (var i = 0; i < 50; i++)
                {
                    System.Threading.Thread.Sleep(1000);
                    WriteLog("WaitSum 执行时间:" + DateTime.Now.TimeOfDay.ToString() + "
    ");
                  
                }
    
                return a + b;
            }
    
    
            /// <summary>
            /// 日志记录
            /// </summary>
            /// <param name="msg"></param>
            public  static void WriteLog(string msg)
            {
                // 此处不能通过 Server.Map 去获取对象
                string realPath = AppDomain.CurrentDomain.BaseDirectory+"/log.txt";
                FileStream fsWriter = null;
                if (!File.Exists(realPath))
                {
                    fsWriter = new FileStream(realPath, FileMode.Create);
                }
                else
                {
                    fsWriter = new FileStream(realPath, FileMode.Append);
                }
                StreamWriter sw = new StreamWriter(fsWriter);//实现一个类,向流中写入字符
                sw.WriteLine(msg);//写入流
                sw.Close();//类关闭
                fsWriter.Close();//流关闭
            }
        }
    }
    

      

    2、调用方法:

      同步:

      

       CounterService.WriteLog("同步调用开始:" + DateTime.Now.TimeOfDay.ToString() + "
    ");
                CounterService counterService=new CounterService ();
                SumDelegate sumDellegate = new SumDelegate(counterService.Sum);
                lbResult.Text=sumDellegate(10,20).ToString();
                CounterService.WriteLog("同步调用完成:" + DateTime.Now.TimeOfDay.ToString() + "
    ");
    

      

      异步:

       

      Action<IAsyncResult> action=(x)=>{
                    CounterService.WriteLog("异步调用Action开始:" + DateTime.Now.TimeOfDay.ToString() + "
    ");
    
                    SumDelegate handler = (SumDelegate)((System.Runtime.Remoting.Messaging.AsyncResult)x).AsyncDelegate;
                    lbResult.Text = handler.EndInvoke(x).ToString() + ";" + x.AsyncState;
                    CounterService.WriteLog("异步调用Action结束:" + DateTime.Now.TimeOfDay.ToString() + "
    ");
              
                };
    
                CounterService.WriteLog("结果输出时间:" + DateTime.Now.TimeOfDay.ToString() + "
    ");
                lbSync.Text = "结果输出,但异步委托还在执行";
    
                CounterService.WriteLog("异步调用开始:" + DateTime.Now.TimeOfDay.ToString() + "
    ");
    
                CounterService counterService = new CounterService();
                SumDelegate sumDellegate = new SumDelegate(counterService.WaitSum);
                IAsyncResult asyncResult = sumDellegate.BeginInvoke(10, 30, new AsyncCallback(action), "OK:" + DateTime.Now.TimeOfDay.ToString());
                CounterService.WriteLog("异步调用完成:" + DateTime.Now.TimeOfDay.ToString() + "
    ");

    代码下载地址:

  • 相关阅读:
    js中的this
    js中的call和apply
    自定义ListView无法响应点击事件
    Listview滑动超出显示区域时getChildAt(arg2)异常错误
    cocos2d-x在eclipse上搭建开发环境
    阿里云服务器CentOS 5.7(64位)安装配置LAMP服务器(Apache+PHP5+MySQL)
    如何在eclipse模拟器上运行下载好的apk程序
    百度RSS整理
    vtigercrm学习(二)
    Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果
  • 原文地址:https://www.cnblogs.com/rhythmK/p/3873481.html
Copyright © 2020-2023  润新知