zoukankan      html  css  js  c++  java
  • .NET 事件

    官方参考文档:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/events/how-to-publish-events-that-conform-to-net-framework-guidelines

    事件是对象用于(向系统中的所有相关组件)广播已发生事情的一种方式。 任何其他组件都可以订阅事件,并在事件引发时得到通知。发送(或引发 )事件的类称为“发布者” ,接收(或处理 )事件的类称为“订阅者” 。

    下例通过使用自定义 EventArgs 类和 EventHandler<TEventArgs> 作为事件类型来演示事件

    namespace DotNetEvents
    {
        using System;
        using System.Collections.Generic;
    
        // Define a class to hold custom event info
        public class CustomEventArgs : EventArgs
        {
            public CustomEventArgs(string s)
            {
                message = s;
            }
            private string message;
    
            public string Message
            {
                get { return message; }
                set { message = value; }
            }
        }
    
        // Class that publishes an event
        class Publisher
        {
    
            // Declare the event using EventHandler<T>
            public event EventHandler<CustomEventArgs> RaiseCustomEvent;
    
            public void DoSomething()
            {
                // Write some code that does something useful here
                // then raise the event. You can also raise an event
                // before you execute a block of code.
                OnRaiseCustomEvent(new CustomEventArgs("Did something"));
    
            }
    
            // Wrap event invocations inside a protected virtual method
            // to allow derived classes to override the event invocation behavior
            protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
            {
                // Make a temporary copy of the event to avoid possibility of
                // a race condition if the last subscriber unsubscribes
                // immediately after the null check and before the event is raised.
                EventHandler<CustomEventArgs> handler = RaiseCustomEvent;
    
                // Event will be null if there are no subscribers
                if (handler != null)
                {
                    // Format the string to send inside the CustomEventArgs parameter
                    e.Message += $" at {DateTime.Now}";
    
                    // Use the () operator to raise the event.
                    handler(this, e);
                }
            }
        }
    
        //Class that subscribes to an event
        class Subscriber
        {
            private string id;
            public Subscriber(string ID, Publisher pub)
            {
                id = ID;
                // Subscribe to the event using C# 2.0 syntax
                pub.RaiseCustomEvent += HandleCustomEvent;
            }
    
            // Define what actions to take when the event is raised.
            void HandleCustomEvent(object sender, CustomEventArgs e)
            {
                Console.WriteLine(id + " received this message: {0}", e.Message);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Publisher pub = new Publisher();
                Subscriber sub1 = new Subscriber("sub1", pub);
                Subscriber sub2 = new Subscriber("sub2", pub);
    
                // Call the method that raises the event.
                pub.DoSomething();
    
                // Keep the console window open
                Console.WriteLine("Press Enter to close this window.");
                Console.ReadLine();
    
            }
        }
    }

     

  • 相关阅读:
    用于Delphi的DevExpress VCL组件——富文本编辑功能升级
    5个技巧,教你优化React App性能
    DevExpress WPF模板库助力快速完成界面美化
    Web开发小技巧放送
    WinForm应用界面开发入门指南
    浅析C#中单点登录的原理和使用
    Wireshark抓包,带你快速入门
    你必须知道的EF知识和经验
    你知道SqlDataAdapter中的Fill是怎么实现的吗
    一文说通C#中的异步编程
  • 原文地址:https://www.cnblogs.com/marshhu/p/11943786.html
Copyright © 2011-2022 走看看