usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceinvoketest{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Public voidUpdateForm (stringPARAM1,stringParm2) { This. TextBox1.Text = param1 +Parm2; } Public Delegate voidUPDATEFORM_DL (stringSTR1,stringstr2); Private voidcalldelegate () {/*Using This.invoke in a Windows Forms application to use This.Dispatcher.Invoke in a WPF application*/ This. BeginInvoke (NewUPDATEFORM_DL (UpdateForm),New Object[] {"I am a text box","haha" }); //This . Dispatcher.begininvoke (New Updateform_dl (UpdateForm), new object[] {"I am text box", "haha"}); } Public voidDoWork () {calldelegate (); } Private voidForm1_Load (Objectsender, EventArgs e) { } Private voidButton1_Click (Objectsender, EventArgs e) {Thread Thread=NewThread (NewThreadStart (DoWork)); Thread. Start (); } }}
Delegate example of a delegate that implements updates to controls in a form