Click here to Skip to main content
13,349,033 members (65,503 online)
Click here to Skip to main content
Add your own
alternative version


92 bookmarked
Posted 5 Apr 2002

The key to multi-threaded Windows Forms UI interaction

, 5 Apr 2002
Rate this:
Please Sign up or sign in to vote.
Teaches how to use the Invoke functionality so that interaction with UI elements can be safely done.
<!-- Article image -->

Sample Image - winformsthreading.jpg

<!-- Add the rest of your HTML here -->


I don't intend for this article to teach multi-threaded programming in .NET, I do want to point out some important things when dealing with multi-threaded programs in a Windows Forms environment.  This article will be code light, however I have provided a full sample illustrating how to employ the practices required of a Multi-Threaded Windows Forms application.

What not to do

First and foremost (and the reason I'm writing this article) is that you absolutely, positively, cannot interact with UI elements from a thread that did not create them.  You may get away with it in some cases, but it will fail at some point and then heads will roll because you will have a lot of code to change.  Now you may ask how should you interact with the UI from another thread. 

What you should do

Luckily the designers for Windows Forms realized the plight that may be caused without being able to interact with the UI from another thread.  So they add the Invoke method to the Control class.  Invoke runs the specified delegate on the thread that create the control's window (or the parents window if it doesn't have one).

public void Invoke(System.Delegate delegate);
public void Invoke(System.Delegate delegate, object [] args);

The second form of Invoke takes an object array containing any parameters you wish to be passed into the delegate.  For those still not used to delegates, think of them as the .NET way to use function pointers.

A brief example

A simple example of using this is to create a new form, add a textbox and a button to it.  Call the textbox myTextbox.

At the top of the code file add another using statement for the threading library.

using System.Threading;

In the button's Click event place the following code.

Thread t = new Thread(new ThreadStart(SayHiThread));
t.IsBackground = true;

Now add this code to the form

private void SayHiThread()
	Invoke(new SayHiDelegate(SayHi), new object [] { "Hello from the thread" });
private void SayHi(string msg)
	myTextbox.Text = msg;

Outside of the form's class add this declaration

public delegate void SayHiDelegate(string msg);

Run the application and click the button, the textbox should now say "Hello from the thread".

What it did

Well that probably confused you more than it helped so I'll try to explain piece by piece what it does.

The Invoke method requires an instance of a delegate.  A delegate is nothing more than a type-safe function pointer.  You declare a delegate by creating the function signature you wish to have, then preceding the signature with the delegate keyword.  In the example above the function will take a string as its only parameter and return nothing.  You create a new delegate instance as if you were creating an instance of a class, except you pass in the name of the method you want the delegate to call.

The code inside of the button's click event creates a new thread, sets it as a background thread, then runs it.  All the thread does is run the function SayHiThread.  The thread only does one thing, it calls Invoke on the form (since SayHiThreadis a member of the form it calls the form's Invoke method).   The Invoke method takes an instance of a delegate, and an object array containing the parameters to give to the delegate.  In this case there is only one parameter; a string. 

To sum it up, Invoke calls the function which the delegate points to, passing in the parameters in the object array.


When you get down to it the hardest part of writing the code to interact with the UI from another thread is getting to understand delegates and how to use them.  As always, if there are any questions e-mail me or post them below. The source/demo show a example that's a bit more advanced, but still nothing too advanced.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

James T. Johnson
Software Developer (Senior) InfoPlanIT, LLC
United States United States
James has been programming in C/C++ since 1998, and grew fond of databases in 1999. His latest interest has been in C# and .NET where he has been having fun writing code starting when .NET v1.0 was in its first beta.

He is currently a senior developer and consultant for InfoPlanIT, a small international consulting company that focuses on custom solutions and business intelligence applications.

He was previously employed by ComponentOne where he was a Product Manager for the ActiveReports, Data Dynamics Reports, and ActiveAnalysis products.

Code contained in articles where he is the sole author is licensed via the new BSD license.

You may also be interested in...

Comments and Discussions

QuestionThank You Pin
myloginer3-Nov-14 2:35
membermyloginer3-Nov-14 2:35 
GeneralTHANK YOU! Pin
SirCthulhu22-Jan-14 9:56
professionalSirCthulhu22-Jan-14 9:56 
QuestionThanks ! Pin
Davide Calabro7-Oct-11 6:49
memberDavide Calabro7-Oct-11 6:49 
Generalthis is not a really multi thread UI Pin
E.T.21-Dec-08 7:04
memberE.T.21-Dec-08 7:04 
GeneralRe: this is not a really multi thread UI Pin
James T. Johnson21-Dec-08 16:36
memberJames T. Johnson21-Dec-08 16:36 
Generalcreating multithreads in C# Pin
alzrrog11-May-07 12:22
memberalzrrog11-May-07 12:22 
GeneralRe: creating multithreads in C# Pin
Patrick Sears11-May-07 12:47
memberPatrick Sears11-May-07 12:47 
GeneralAvoid Invoke() - prefer BeginInvoke() Pin
Kristof Verbiest27-Feb-07 4:02
memberKristof Verbiest27-Feb-07 4:02 
GeneralVery useful and easy to follow! Thanks! Pin
anichin17-Mar-06 7:12
memberanichin17-Mar-06 7:12 
GeneralVery Helpful Pin
Tim Friesen23-Aug-05 7:54
memberTim Friesen23-Aug-05 7:54 
GeneralAccessing UI Element's Properties Pin
JohnCBowman27-Apr-04 3:10
memberJohnCBowman27-Apr-04 3:10 
GeneralRe: Accessing UI Element's Properties Pin
S. Senthil Kumar18-May-05 23:27
memberS. Senthil Kumar18-May-05 23:27 
GeneralSame concept, different location Pin
XiNull9-Dec-03 17:38
memberXiNull9-Dec-03 17:38 
GeneralRe: Same concept, different location Pin
Todd Gray21-Dec-04 8:16
memberTodd Gray21-Dec-04 8:16 
GeneralRe: Same concept, different location Pin
S. Senthil Kumar18-May-05 23:24
memberS. Senthil Kumar18-May-05 23:24 
Generalchanging the value of a boolean when a loop is going on Pin
pasous6-Nov-03 12:42
memberpasous6-Nov-03 12:42 
GeneralRe: changing the value of a boolean when a loop is going on Pin
James T. Johnson7-Nov-03 6:07
memberJames T. Johnson7-Nov-03 6:07 
Questionwrong code on Pin
Cristian Balcanu19-Apr-03 8:23
sussCristian Balcanu19-Apr-03 8:23 
AnswerRe: wrong code on Pin
James T. Johnson19-Apr-03 12:52
memberJames T. Johnson19-Apr-03 12:52 
GeneralCannot use Invoke if calling Join inside Closing event - causes deadlock Pin
JoergB13-Apr-03 22:26
memberJoergB13-Apr-03 22:26 
GeneralRe: Cannot use Invoke if calling Join inside Closing event - causes deadlock Pin
James T. Johnson14-Apr-03 7:14
memberJames T. Johnson14-Apr-03 7:14 
GeneralRe: Cannot use Invoke if calling Join inside Closing event - causes deadlock Pin
JoergB14-Apr-03 20:00
memberJoergB14-Apr-03 20:00 
GeneralRe: Cannot use Invoke if calling Join inside Closing event - causes deadlock Pin
James T. Johnson14-Apr-03 20:52
memberJames T. Johnson14-Apr-03 20:52 
GeneralRe: Cannot use Invoke if calling Join inside Closing event - causes deadlock Pin
JoergB14-Apr-03 21:10
memberJoergB14-Apr-03 21:10 
GeneralRe: Cannot use Invoke if calling Join inside Closing event - causes deadlock Pin
James T. Johnson15-Apr-03 7:11
memberJames T. Johnson15-Apr-03 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 6 Apr 2002
Article Copyright 2002 by James T. Johnson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid