C# callback function in detail

What are callbacks and callback functions

There are interfaces between software modules, and callback is a two-way calling mode. The callee will also call the other party's interface when the interface is called.
For general structured languages, callback functions can be used to implement callbacks. A callback function is a function or procedure, which is implemented by the caller and used by the callee.
For C and C++, the call is generally implemented through function pointers.
For C#, the declared delegate event is like a pointer. Another method is called through the event variable, and the method is passed to another method as a parameter. The callback function is a part of a workflow, and the workflow determines the timing of the function call (callback).

How to achieve it?

Implement callbacks by declaring delegates.
We often encounter event-triggered processes in C# programming, and the way to define this event is the callback function. The callback function is actually the process of one method calling another method, which is implemented through a delegate, not necessarily an event or a delegate variable.
First look at a typical C# delegate declaration code:

public delegate void EventHandler( object sender , EventArgs e )

Among them, object sender: represents the control object that triggered the event, and the sender parameter is used to pass a reference to the event source object. Use sender to find out the specific control that triggered the event.
EventArgs is the base class of the class containing event data, used to convey the details of the event, and is the information attached to the event.

using System;

namespace 回调函数
{
    //1.声明关于事件的委托;
    public delegate void AlarmEventHandler(object sender, EventArgs e);
    class Dog
    {
        //2.声明事件;   
        public event AlarmEventHandler Alarm;

        //3.编写引发事件的函数;
        public void OnAlarm()
        {
            if (this.Alarm != null)
            {
                Console.WriteLine("\n汪汪~~~~~~~");
                this.Alarm(this, new EventArgs());   //发出警报
            }
        }
    }

    //事件接收者
    class Host
    {
        //4.编写事件处理程序
        void HostHandleAlarm(object sender, EventArgs e)
        {
            Console.WriteLine("主人: 吓死宝宝!");
        }

        //5.注册事件处理程序
        public Host(Dog dog)
        {
            dog.Alarm += new AlarmEventHandler(HostHandleAlarm);
        }
    }

    class Thief
    {
        void ThiefHandleAlarm(object sender, EventArgs e)
        {
            Console.WriteLine("小偷: 吓死宝宝!");
        }
        public Thief(Dog dog)
        {
            dog.Alarm += new AlarmEventHandler(ThiefHandleAlarm);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            Host host = new Host(dog);
            Thief thief = new Thief(dog);
            //当前时间,从2021年3月9日23:59:50开始计时
            DateTime now = new DateTime(2021, 3, 9, 23, 59, 55);
            DateTime midnight = new DateTime(2021, 3, 10, 0, 0, 0);

            //等待午夜的到来
            Console.WriteLine("时间一秒一秒地流逝... ");
            while (now < midnight)
            {
                Console.WriteLine("当前时间: " + now);
                System.Threading.Thread.Sleep(1000);    //程序暂停一秒
                now = now.AddSeconds(1);                //时间增加一秒
            }
            Console.WriteLine("\n月黑风高的午夜: " + now);
            Console.WriteLine("小偷悄悄地摸进了女主人的屋内... ");
            dog.OnAlarm();
            Console.ReadLine();
        }
    }
}

Reference article:
Understanding C# callback function
C# (event trigger) callback function, perfect handling of all kinds of incurable diseases!

Guess you like

Origin blog.csdn.net/baidu_35536188/article/details/114629788