C # asynchronous Begininvoke,
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace WindowsFormsApplication1 8 {9 public class delegateclass10 {11 private delegate int Dothread (); 12 static Dothread dothread = new Dothread (work); 13 14 public static void start () 15 {16 AsyncCallback backCall = new AsyncCallback (backcall );
18 dothread. beginInvoke (backCall, "parameter for asynchronous call"); // The first parameter is the parameter for calling the work method, and the second is the callback function, the third parameter that needs to be passed to the callback function can be Object type 19} 20 // <summary> 21 // This is the delegate call program 22 /// </summary> 23 private static int work () 24 {25 System. windows. forms. messageBox. show ("I'm a delegate caller"); 26 return 999; 27 28} 29 // <summary> 30 // callback function 31 // </summary> 32 // <param name = "parameter"> </param> 33 // <returns> </returns> 34 private static int backcall (int parameter) 35 {36 System. windows. forms. messageBox. show ("this is a callback function"); 37 return 0; 38 39} 40 private static void backcall (IAsyncResult parameter) 41 {42 int result = dothread. endInvoke (parameter); 43 System. windows. forms. messageBox. show ("this is a callback function"); 44 System. windows. forms. messageBox. show (result. toString (); 45 System. windows. forms. messageBox. show (parameter. asyncState. toString (); 46 47 48} 49} 50 51}
The above is a created class. The name is casual due to a small amount of time.
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms;10 11 namespace WindowsFormsApplication112 {13 public partial class Form1 : Form14 {15 public Form1()16 {17 InitializeComponent();18 }19 20 private void button1_Click(object sender, EventArgs e)21 {22 delegateclass.start();23 }24 }25 }