Click here to Skip to main content
13,045,379 members (124,391 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 19 Feb 2007

Updating the GUI from another thread made easy

, 19 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Ever tried updating a control from a background thread and received an error? Here's an easy and clean way to do it...


I've found out to my absolute horror that even simple operations done in a background thread that need to update the interface are required to force those interface calls back into the same thread as the interface was generated in…

With a bit of research, I found out that this was done with the Invoke method. Initially, I created what felt like hundreds of delegates/functions to handle each control's update, but now, although this solution I have posted could be better, it is a decent timesaver at least for me, so hopefully it will help someone else…

Basically, what we have below is a static class (ThreadSafe.cs) that has some delegates such as SetText(Control, string) that lets you set the text of any control with some text. The following example is rather basic, but there are numerous others in ThreadSafe.cs such as adding items to listviews, changing checkbox check states etc. Give it a look.

Here is a basic example for changing the Text property of a control.


ThreadSafe.SetText(this.whateverControl, "text to change");

couldn't be easier.

The Delegate

public delegate void SetTextDelegate(System.Windows.Forms.Control ctrl, string text);

This defines the signature of the SetText method.

The Method


public static void SetText(System.Windows.Forms.Control ctrl, string text)

    if (ctrl.InvokeRequired)
        object[] params_list = new object[] { ctrl, text };
        ctrl.Invoke(new SetTextDelegate(SetText), params_list);
        ctrl.Text = text;

There are also classes and inherited classes for other controls, listviews, buttons, comboboxes etc., that should save you time writing your thread-safe GUI code. Hope this helps someone. If it does or for help, please leave a comment!

Download Helper Class


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Europe Europe
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGreat! Thanks Pin
Member 33242724-Aug-10 7:11
memberMember 33242724-Aug-10 7:11 
GeneralTHANK YOU!!!!!!!!!!!!!!!!! Pin
vigylant8-Oct-08 7:47
membervigylant8-Oct-08 7:47 
GeneralUsing backgroundworker for update GUI Pin
alhambra-eidos7-Aug-08 21:05
memberalhambra-eidos7-Aug-08 21:05 
Questionfor web project? Pin
pravin parmar27-Jul-08 6:49
memberpravin parmar27-Jul-08 6:49 
Generalsimple questions Pin
newbie_to_csharp30-Jun-08 12:07
membernewbie_to_csharp30-Jun-08 12:07 
GeneralA better solution for CLI (probabelly) Pin
ShlomiO21-May-08 21:33
memberShlomiO21-May-08 21:33 
QuestionIsn't it time this was done by the controls themselves? Pin
simon heffer19-Oct-07 3:12
membersimon heffer19-Oct-07 3:12 
GeneralNET Threading issues Pin
CodeWatch5-Mar-07 18:25
memberCodeWatch5-Mar-07 18:25 
GeneralNice article Pin
--=A J E E S H=--19-Feb-07 17:33
member--=A J E E S H=--19-Feb-07 17:33 
GeneralAs some people mentioned... Pin
Itay Sagui19-Feb-07 10:51
memberItay Sagui19-Feb-07 10:51 
GeneralRe: As some people mentioned... Pin
Robert Ensor21-Feb-07 14:30
memberRobert Ensor21-Feb-07 14:30 
GeneralAnonymous Delegates Pin
sirlantis19-Feb-07 8:13
membersirlantis19-Feb-07 8:13 
GeneralRe: Anonymous Delegates Pin
Oleg Shilo19-Feb-07 15:31
memberOleg Shilo19-Feb-07 15:31 
GeneralRe: Anonymous Delegates Pin
daniel.byrne20-Feb-07 0:44
memberdaniel.byrne20-Feb-07 0:44 
GeneralRe: Anonymous Delegates Pin
daniel.byrne20-Feb-07 1:07
memberdaniel.byrne20-Feb-07 1:07 
GeneralRe: Anonymous Delegates Pin
Oleg Shilo20-Feb-07 2:33
memberOleg Shilo20-Feb-07 2:33 
AnswerRe: Anonymous Delegates Pin
Robert Ensor20-Feb-07 21:32
memberRobert Ensor20-Feb-07 21:32 
GeneralRe: Anonymous Delegates Pin
Oleg Shilo21-Feb-07 14:23
memberOleg Shilo21-Feb-07 14:23 
GeneralA similar method... Pin
Neal Sanche6-Mar-07 18:06
memberNeal Sanche6-Mar-07 18:06 
Generalsee SynchronizationContext Pin
_SAM_19-Feb-07 5:30
member_SAM_19-Feb-07 5:30 
GeneralRe: see SynchronizationContext Pin
Patrick Sears19-Feb-07 6:26
memberPatrick Sears19-Feb-07 6:26 
GeneralHi guys, if you like this.. Pin
daniel.byrne19-Feb-07 5:17
memberdaniel.byrne19-Feb-07 5:17 
GeneralRe: Hi guys, if you like this.. [modified] Pin
bombdrop19-Feb-07 5:30
memberbombdrop19-Feb-07 5:30 
GeneralRe: Hi guys, if you like this.. Pin
daniel.byrne19-Feb-07 6:20
memberdaniel.byrne19-Feb-07 6:20 
GeneralRe: Hi guys, if you like this.. Pin
daniel.byrne19-Feb-07 6:22
memberdaniel.byrne19-Feb-07 6:22 

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
Web02 | 2.8.170713.1 | Last Updated 19 Feb 2007
Article Copyright 2007 by daniel.byrne
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid