Explain in detail the method of realizing callback function function through delegate in C

  • 2021-09-24 23:21:37
  • OfStack

A delegate (delegate) is a type that can store references as functions, similar to function pointers in c + +.

Callback function
The callback function in c + + is realized by function pointer. Similarly, in c #, delegates are used to realize the function of callback function.

Why are callback functions called callback functions? For example, if you call a function, it is called a call, but if you need to provide a function to call your function when you call a function, then the function you provide is called a callback function (callback).

For dynamic languages like python, there is no special syntax for c # and c + + to implement callback functions, because in python, the function is also an object, and there will be no problem when the parameters are passed or when the return value of the function is returned.

The callback function is also used as a "plug-in":

In the STL library of C + +, the specific algorithm of sorting function has been implemented, but the specific comparison method of two elements is provided by callback function (comparison function), which ensures that the algorithm can be used for different types such as int and string.


void sort( iterator start, iterator end );
void sort( iterator start, iterator end, StrictWeakOrdering cmp );

c # Delegate
Delegates are often used in event handling. Because you can delegate the specific operation (what to do) after the event is triggered to the implementation class. This is the Hollywood rule, "Don 't call me, I will call you".

This example comes from c # Classic Getting Started:


delegate double ProcessDelegate(double param1, double param2);

static double Multiply(double param1, double param2)
{
  return param1 * param2;
}

staitc double Divide(double param1, double param2)
{
  return param1 / param2;
}

if (input == "M")
  process = new ProcessDelegate(Multiply);
else
  process = new ProcessDelegate(Divide);

However, in the real situation, we do not initialize the callback function through logical judgment. The following example of this function plug-in is more common.


static void ExecuteFunction(ProcessDelegate process)
{
process(2.2, 3.3);
}

An example of an C # delegate implementing a callback:


using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
 
namespace Stockes 
{ 
  public partial class CallBack : Form 
  { 
    public CallBack() 
    { 
      InitializeComponent(); 
      // Initialize callback method  
      writeBoxCallback1 = new WriteBoxCallback1(Write1); 
      writeBoxCallbacK2 = new WriteBoxCallback2(Write2); 
    } 
    // Declare that the delegate has implemented the callback mechanism  
    private delegate void WriteTextBox(char ch); 
    private WriteTextBox writeTextBox; 
     //Text1 Callback of  
    private delegate void WriteBoxCallback1(char ch); 
    private WriteBoxCallback1 writeBoxCallback1; 
    //Text2 Callback of  
    private delegate void WriteBoxCallback2(char ch); 
    private WriteBoxCallback2 writeBoxCallbacK2; 
    private void button1_Click(object sender, EventArgs e) 
    { 
      if (checkBox1.Checked) 
      { 
        groupBox4.Text = " Running. . . "; 
        groupBox4.Refresh(); 
        textBox1.Clear(); 
        textBox1.Refresh(); 
        Thread th1 = new Thread(new ThreadStart(dowrite1));// Create Thread 1 
        th1.Start();// Startup thread 1 
      } 
      if (checkBox2.Checked) 
      { 
        groupBox2.Refresh(); 
        groupBox5.Text = " Running. . . "; 
        groupBox5.Refresh(); 
        textBox2.Clear(); 
        textBox2.Refresh(); 
        Thread th2 = new Thread(new ThreadStart(dowrite2));// Create Thread 2 
        th2.Start();// Startup thread 2 
      } 
    } 
    //Text1 Using callback statements  
    private void CallTex1(char ch) 
    { 
      textBox1.Invoke(writeBoxCallback1,ch); 
    } 
    //Text2 Using callback statements  
    private void CallText2(char ch) 
    { 
      textBox2.Invoke(writeBoxCallbacK2,ch); 
    } 
    // Using delegates  
    private void WriteTex(WriteTextBox write) 
    { 
      string str = textBox3.Text.Trim(); 
      for (int i = 0; i < str.Length; i++) 
      { 
        write(str[i]); 
        DateTime now = DateTime.Now; 
        while (now.AddSeconds(1) > DateTime.Now) { } 
      } 
    } 
    //Text1 Add value  
    private void Write1(char ch) 
    { 
      textBox1.AppendText(ch+"\r"); 
    } 
    //Text2 Add value  
    private void Write2(char ch) 
    { 
      textBox2.AppendText(ch+"\r"); 
    } 
    // Thread 1 Called method  
    private void dowrite1() 
    { 
      if (checkBox1.Checked) 
      { 
        writeTextBox = new WriteTextBox(CallTex1); 
        WriteTex(writeTextBox); 
      } 
    } 
    // Thread 2 Called method  
    private void dowrite2() 
    { 
      if (checkBox2.Checked) 
      { 
        writeTextBox = new WriteTextBox(CallText2); 
        WriteTex(writeTextBox); 
      } 
    } 
  } 
} 

Related articles: