usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewindowsformsapplication1{ Public Partial classForm1:form {Delegate voidDelshow (stringstrinput); Delshow Delshow=NULL; PublicForm1 () {InitializeComponent (); Delshow=Newdelshow (myshow); //control.checkforillegalcrossthreadcalls = false; } Private voidbtnTest_Click (Objectsender, EventArgs e) {System.Threading.Thread T1=NewSystem.Threading.Thread (() = { if( This. invokerequired) {#regionWay One//This . Invoke (new action<string> (s = = {This.label1.Text = s;}), "BBB"); #endregion #regionWay Two This. Invoke (Delshow,"CCC"); #endregion } Else { This. Label1. Text ="AAA"; } }); T1. IsBackground=true; T1. Start (); } Private voidMyshow (stringstrinput) { This. Label1. Text =strinput; } }}
WinForm accessing controls across threads