private void button1_Click(object sender, EventArgs e) { // Execute the SetButtonText method on the main thread button1.Invoke(new Action(SetButtonText)); } private void SetButtonText() { // This method will run on the main UI thread button1.Text = "Button Clicked!"; }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // This method runs on a background thread // Do some long-running task here // Then update a label on the main UI thread label1.Invoke(new Action(() => { label1.Text = "Task completed!"; })); }In this example, we are using the BackgroundWorker class to run a long-running task on a background thread. Once the task is completed, we are using the Button.Invoke method to update a label on the main UI thread with the text "Task completed!". Overall, the System.Windows.Forms.Button.Invoke method is a powerful tool for updating UI controls from background threads. It's part of the .NET Framework Windows Forms library.