zoukankan      html  css  js  c++  java
  • C#_delegate

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace EventClock
    {
    
        public class ClassWithDelegate
        { 
            //封装了一个返回值为int的多重委托方法
            public delegate int DelegateThatReturns();
    
            public event DelegateThatReturns theDelegate;
    
            public void Run()
            {
                for (; ; )
                {
                    Thread.Sleep(500);
    
                    if (theDelegate != null)
                    {
                        //显式调用每个委托方法
                        foreach (DelegateThatReturns del in theDelegate.GetInvocationList())
                        {
                            //异步调用
                            //传入委托作为一个状态对象
                            del.BeginInvoke(new AsyncCallback(ResultReturns), del);
                           
                        }
                        Console.WriteLine();
                    }
                }
            }
            //获取结果的回调方法
            public void ResultReturns(IAsyncResult iar)
            {
                //将状态对象转换为委托类型
                DelegateThatReturns del = (DelegateThatReturns)iar.AsyncState;
                //调用委托的EndInvoke方法获取结果
                int result = del.EndInvoke(iar);
                //显示结果
                Console.WriteLine("Delegate returned result: {0}",result);
            }
        }
    
        public class FirstSubscribe
        {
            private int myCounter = 0;
    
            public void Subscribe(ClassWithDelegate theClassWithDelegate)
            {
                theClassWithDelegate.theDelegate += new ClassWithDelegate.DelegateThatReturns(DisplayCounter);
            }
    
            public int DisplayCounter()
            {
                Console.WriteLine("Busy in displaying............");
                Thread.Sleep(10000);
                Console.WriteLine("Do not with work in display............");
                return ++myCounter;
            }
        }
        public class SecondSubscribe
        {
            private int myCounter = 0;
    
            public void Subscribe(ClassWithDelegate theClassWithDelegate)
            {
                theClassWithDelegate.theDelegate += new ClassWithDelegate.DelegateThatReturns(Doubler);
            }
    
            public int Doubler()
            {
                return myCounter += 2;
            }
        }
    
    
        
    
        class Program
        {
            static void Main(string[] args)
            {
                ClassWithDelegate theClassWithDelegate = new ClassWithDelegate();
    
                FirstSubscribe fs = new FirstSubscribe();
                fs.Subscribe(theClassWithDelegate);
    
                SecondSubscribe ss = new SecondSubscribe();
                ss.Subscribe(theClassWithDelegate);
    
                theClassWithDelegate.Run();
                
                Console.ReadLine();
            }
        }
    }
    


  • 相关阅读:
    Java——异步调用
    GTK3-demo 代码调用
    ef core code first 生成的数据库表去复数的方法
    nuxt全局挂载导航路由守卫
    vue导入,导出,列表展示excel数据
    JS之blob对象下载文件,解决word可能打开是乱码,xlsx文件打不开,图片显示格式不支持等问题
    程序猿的十一条浮躁表现
    RSA加密解密及加签验签
    冒泡排序
    Failed to parse source for import analysis because the content contains invalid JS syntax
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720443.html
Copyright © 2011-2022 走看看