SoFunction
Updated on 2025-03-08

Detailed explanation of the method of implementing callback function function in C#

Delegate is a type that can store references as functions, similar to function pointers in C++.

Callback function
The callback function in c++ is implemented using function pointers. Similarly, delegates are used in C# to implement the function of callback functions.

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 the function and let this function call your function, then the function you provide is called a callback function.

For dynamic languages ​​like python, there is no c#. C++ provides special syntax to implement callback functions, because in python, a function is also an object, and there will be no problem whether it is passed when a parameter is passed or when the function returns a value.

The callback function is also used as a "plugin":

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

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

c# delegation
Delegations are often used in event processing. Because the specific operations after the event is triggered (what should be done) can be delegated to the implementation class. This is the Hollywood rule, "Don't call me, I will call you".

This example comes from the classic introduction to C#:

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);

But we do not use logical judgment to initialize the callback function. The following example of the function plug-in is more common.

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

An example of C# delegate implementation of callbacks:

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
 
namespace Stockes 
{ 
  public partial class CallBack : Form 
  { 
    public CallBack() 
    { 
      InitializeComponent(); 
      //Initialize callback method      writeBoxCallback1 = new WriteBoxCallback1(Write1); 
      writeBoxCallbacK2 = new WriteBoxCallback2(Write2); 
    } 
    //Declare the delegate to implement the callback mechanism    private delegate void WriteTextBox(char ch); 
    private WriteTextBox writeTextBox; 
     //Text1's callback    private delegate void WriteBoxCallback1(char ch); 
    private WriteBoxCallback1 writeBoxCallback1; 
    //Text2's callback    private delegate void WriteBoxCallback2(char ch); 
    private WriteBoxCallback2 writeBoxCallbacK2; 
    private void button1_Click(object sender, EventArgs e) 
    { 
      if () 
      { 
         = "Running...."; 
        (); 
        (); 
        (); 
        Thread th1 = new Thread(new ThreadStart(dowrite1));//Create thread 1        ();//Start thread 1      } 
      if () 
      { 
        (); 
         = "Running...."; 
        (); 
        (); 
        (); 
        Thread th2 = new Thread(new ThreadStart(dowrite2));//Create thread 2        ();//Start thread 2      } 
    } 
    //Text1 uses callback statement    private void CallTex1(char ch) 
    { 
      (writeBoxCallback1,ch); 
    } 
    //Text2 uses callback statement    private void CallText2(char ch) 
    { 
      (writeBoxCallbacK2,ch); 
    } 
    //Use Delegation    private void WriteTex(WriteTextBox write) 
    { 
      string str = (); 
      for (int i = 0; i < ; i++) 
      { 
        write(str[i]); 
        DateTime now = ; 
        while ((1) > ) { } 
      } 
    } 
    //Text1 adds a value    private void Write1(char ch) 
    { 
      (ch+"\r"); 
    } 
    //Text2 add value    private void Write2(char ch) 
    { 
      (ch+"\r"); 
    } 
    //Methods called by thread 1    private void dowrite1() 
    { 
      if () 
      { 
        writeTextBox = new WriteTextBox(CallTex1); 
        WriteTex(writeTextBox); 
      } 
    } 
    //Methods called by thread 2    private void dowrite2() 
    { 
      if () 
      { 
        writeTextBox = new WriteTextBox(CallText2); 
        WriteTex(writeTextBox); 
      } 
    } 
  } 
}