Click here to Skip to main content
15,881,938 members
Articles / Programming Languages / C#

Updating the GUI from Another Thread Made Easy

Rate me:
Please Sign up or sign in to vote.
3.09/5 (19 votes)
19 Feb 2007CPOL1 min read 100.8K   952   50   26
Ever tried updating a control from a background thread and received an error? Here's an easy and clean way to do it...

Introduction

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.

Usage

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

couldn't be easier.

The Delegate

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

This defines the signature of the SetText method.

The Method

C#
//generic system.windows.forms.control

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);
    }
    else
    {
        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

License

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


Written By
Europe Europe
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank You Pin
Member 1062893430-Nov-17 4:32
Member 1062893430-Nov-17 4:32 
GeneralGreat! Thanks Pin
Member 33242724-Aug-10 7:11
Member 33242724-Aug-10 7:11 
GeneralTHANK YOU!!!!!!!!!!!!!!!!! Pin
vigylant8-Oct-08 7:47
vigylant8-Oct-08 7:47 
GeneralUsing backgroundworker for update GUI Pin
kiquenet.com7-Aug-08 21:05
professionalkiquenet.com7-Aug-08 21:05 
Questionfor web project? Pin
pravin parmar27-Jul-08 6:49
pravin parmar27-Jul-08 6:49 
Generalsimple questions Pin
newbie_to_csharp30-Jun-08 12:07
newbie_to_csharp30-Jun-08 12:07 
GeneralA better solution for CLI (probabelly) Pin
ShlomiO21-May-08 21:33
ShlomiO21-May-08 21:33 
QuestionIsn't it time this was done by the controls themselves? Pin
simon heffer19-Oct-07 3:12
simon heffer19-Oct-07 3:12 
GeneralNET Threading issues Pin
Coder_20075-Mar-07 18:25
Coder_20075-Mar-07 18:25 
GeneralNice article Pin
--=A J E E S H=--19-Feb-07 17:33
--=A J E E S H=--19-Feb-07 17:33 
Hi,
Nice article Smile | :)

Regards,
--=A J E E S H=--

GeneralAs some people mentioned... Pin
Itay Sagui19-Feb-07 10:51
Itay Sagui19-Feb-07 10:51 
GeneralRe: As some people mentioned... Pin
Robert Ensor21-Feb-07 14:30
Robert Ensor21-Feb-07 14:30 
GeneralAnonymous Delegates Pin
sirlantis19-Feb-07 8:13
sirlantis19-Feb-07 8:13 
GeneralRe: Anonymous Delegates Pin
Oleg Shilo19-Feb-07 15:31
Oleg Shilo19-Feb-07 15:31 
GeneralRe: Anonymous Delegates Pin
daniel.byrne20-Feb-07 0:44
daniel.byrne20-Feb-07 0:44 
GeneralRe: Anonymous Delegates Pin
daniel.byrne20-Feb-07 1:07
daniel.byrne20-Feb-07 1:07 
GeneralRe: Anonymous Delegates Pin
Oleg Shilo20-Feb-07 2:33
Oleg Shilo20-Feb-07 2:33 
AnswerRe: Anonymous Delegates Pin
Robert Ensor20-Feb-07 21:32
Robert Ensor20-Feb-07 21:32 
GeneralRe: Anonymous Delegates Pin
Oleg Shilo21-Feb-07 14:23
Oleg Shilo21-Feb-07 14:23 
GeneralA similar method... Pin
Neal Sanche6-Mar-07 18:06
Neal Sanche6-Mar-07 18:06 
Generalsee SynchronizationContext Pin
_SAM_19-Feb-07 5:30
_SAM_19-Feb-07 5:30 
GeneralRe: see SynchronizationContext Pin
Patrick Etc.19-Feb-07 6:26
Patrick Etc.19-Feb-07 6:26 
GeneralHi guys, if you like this.. Pin
daniel.byrne19-Feb-07 5:17
daniel.byrne19-Feb-07 5:17 
GeneralRe: Hi guys, if you like this.. [modified] Pin
burrows.stephen19-Feb-07 5:30
burrows.stephen19-Feb-07 5:30 
GeneralRe: Hi guys, if you like this.. Pin
daniel.byrne19-Feb-07 6:20
daniel.byrne19-Feb-07 6:20 

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.