Home
Manage Your Code
Snippet: Use Invoke to update user interface from a separate thread (C#)
Title: Use Invoke to update user interface from a separate thread Language: C#
Description: In Windows Forms if you launch a new thread and want to update controls on your form, you must use Invoke. This example updates the Text property of a Label. Views: 211
Author: Aaron Crandall Date Added: 8/29/2007
Copy Code  
1------------------------
2First create a method to make the UI updates, and define a delegate that 
3matches the signature of the method.
4------------------------
5private delegate void setStatusDelegate(string status);
6
7private void setStatus(string status)
8{
9	lblStatus.Text = status;
10}
11
12------------------------
13From another thread, you can use the Invoke method of the form to
14execute the setStatus method.
15------------------------
16Invoke(new setStatusDelegate(setStatus), "This is the status text");