.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() + "\r\n");

              

            }



            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() + "\r\n");

            CounterService counterService=new CounterService ();

            SumDelegate sumDellegate = new SumDelegate(counterService.Sum);

            lbResult.Text=sumDellegate(10,20).ToString();

            CounterService.WriteLog("同步调用完成:" + DateTime.Now.TimeOfDay.ToString() + "\r\n");

  

  异步:

   

  Action<IAsyncResult> action=(x)=>{

                CounterService.WriteLog("异步调用Action开始:" + DateTime.Now.TimeOfDay.ToString() + "\r\n");



                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() + "\r\n");

          

            };



            CounterService.WriteLog("结果输出时间:" + DateTime.Now.TimeOfDay.ToString() + "\r\n");

            lbSync.Text = "结果输出,但异步委托还在执行";



            CounterService.WriteLog("异步调用开始:" + DateTime.Now.TimeOfDay.ToString() + "\r\n");



            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() + "\r\n");

代码下载地址:

 

你可能感兴趣的:(.net)