Delegate callback is the most troublesome thing when I first contact c# it. I always look at it and forget it. So I sorted out my current understanding of delegate callback. If there is any error, please point it out and thank you.
entrust
Delegates in C # are similar to pointers to functions in C or C + +. A Delegate is a reference type variable that holds a reference to a method. References can be changed at run time.
Callback function
Callback function allows the user to pass the pointer of the method to be called as a parameter to a function, so that the function can flexibly use different methods when dealing with similar events.
Note: the defined callback method signature must be consistent with the delegate object, because the compiler will detect their compatibility when binding methods to delegates. If it does not match, it will report a compilation error. For example, if there is a method to pass in a String type, we pass it an int type.
Role of delegate callback:
When called, a delegate can call multiple methods. This is called multicast. To add another method to the delegate's method list (call list), simply add two delegates using the addition operator or the addition assignment operator ("+" or "+ =").
Callback functions can separate the caller from the callee, so the caller doesn't care who the callee is and how the callee implements it. It only needs to know that there is a called function with specific prototypes and constraints.
Code example:
public static void add(int a,int b) { Console.WriteLine(a + b); } public static void multiply(int a,int b) { Console.WriteLine(a * b); } static void Main(string[] args) { int x = 1, y = 3; add(x, y); multiply(x, y); Console.ReadLine(); }
Entrusted version:
//Statement of entrustment
public delegate void CalculateDelegate(int a, int b); public static CalculateDelegate calculateDelegate;
//Callback function 1 public static void add(int a,int b) { Console.WriteLine(a + b); }
//Callback function 2 public static void multiply(int a,int b) { Console.WriteLine(a * b); } static void Main(string[] args) { int x = 1, y = 3; calculateDelegate = add;//Binding event calculateDelegate += multiply;//Multicast calculateDelegate(x, y); Console.ReadLine(); }
Example of delegate callback encountered in project code
The main job of the event sender is to listen. When a critical condition is established, the event receiver will be informed of the event, and the event receiver will complete the subsequent actions.
Example 1: transferring data between windows
The annotation tool I made, imitating labelimg, will be passed back to the main window after entering label in the sub window. I use delegation (I'm not sure if there are other good methods).
(1) Event Publisher: child window
//Delegation of declarative events public delegate void LabelNameDelegate(string labelName); public LabelNameDelegate labelNameDelegate; //Function that raises the event private void btnOK_Click(object sender, EventArgs e) { ... if (labelNameDelegate != null) labelNameDelegate(txtLabelName.Text.Trim()); ... }
(2) Event listener: main window
//Binding event form.labelNameDelegate = addLabelToList; //Handler after event private void addLabelToList(string labelName) { listboxLabel.Items.Add(labelName); ... }
Example 2: image acquisition by camera
In practical work, the most common example is the delegate callback of the Camera. The Camera code is provided by the Camera manufacturer. We only need to call the method without knowing how it processes the data. For example, the Camera will provide a delegate to start capturing, which will return the image data, and then we can process the image data, such as directly displaying it on the picturebox. What we want to write is the corresponding processing function after obtaining the image, such as the following getimage, whose parameters are the same as the delegate parameters. The most important thing is to bind the event, that is, Camera Acqfun = GetImage. At this time, once the Camera starts to acquire the image event, the image will be transferred to the getimage function, processed and displayed in the picturebox.
(1) Event Publisher: camera
When the camera is turned on, the StartAcq event of camera class will be triggered. Event subscribers process events using the method getimage (get pictures from image data).
public class Camera
{ //1. Delegate to declare event public delegate void AcqDelegate(IntPtr imgPtr,int imgHeight,int imgWidth); //2. Declare event public AcqDelegate AcqFun; //3. Write the function that raises the event (the event is usually triggered in a function) public override bool StartAcq() { ... if(AcqFun!=null) AcqFun(); ... } }
Event subscriber
public class xxx { //4. Writing event handlers public void GetImage(IntPtr imgdata, int imageHeight, int imageWidth) { ... } public void StartCamera(Camera.AcqDelegate acqfun) { //5. Register event handlers camera.AcqFun = acqfun; //6. Trigger event camera.StartAcq(); } }
Trigger event
StartCamera(GetImage);
reference resources:
https://www.cnblogs.com/jiangshuai52511/p/7600472.html