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;namespace windowsformsapplication1{public partial Class Form1:form {public delegate void Deletask (int num1, int num2); Public Form1 () {InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) {deletask task = new Deletask (test); int num30 = Int. Parse (This.textBox1.Text); int num40 = Int. Parse (This.textBox2.Text); Task. BeginInvoke (NUM30, NUM40, NULL, NULL); } private void Test (int num1, int num2) {int sum = 0; for (int i = NUM1; I <= num2; i++) {sum + = NUM1; num1++; } this.label1.Text = Sum. ToString (); private void Form1_Load (object sender, EventArgs e) {control.checkforillegalcrossthreadcalls = false; } }}
C # Delegate thread return value 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;namespace WindowsFormsApplication1{ Public partial class Form1:form {public delegate int deletask (int num1, int num2); Public Form1 () {InitializeComponent (); } private void Button1_Click (object sender, EventArgs e) {deletask task = test; int num3 = Int. Parse (This.textBox1.Text); int num4 = Int. Parse (This.textBox2.Text); Task. BeginInvoke (num3, num4, NULL, NULL); IAsyncResult res = task. BeginInvoke (num3, num4, NULL, NULL); int result = task. EndInvoke (RES); This.label1.Text = result. ToString (); } private int Test (int num1, int num2) {int sum = 0; for (int i = NUM1; I <= num2; i++) {sum + = nUM1; num1++; } return sum; private void Form1_Load (object sender, EventArgs e) {control.checkforillegalcrossthreadcalls = False } }}
C # Beginlnvoke Delegate