Click here to Skip to main content
Click here to Skip to main content

Updating the GUI from another thread made easy

, 19 Feb 2007 CPOL
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...

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

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

//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)

Share

About the Author

daniel.byrne

Europe Europe
No Biography provided

Comments and Discussions

 
GeneralGreat! Thanks PinmemberMember 33242724-Aug-10 8:11 
GeneralTHANK YOU!!!!!!!!!!!!!!!!! Pinmembervigylant8-Oct-08 8:47 
GeneralUsing backgroundworker for update GUI Pinmemberalhambra-eidos7-Aug-08 22:05 
Questionfor web project? Pinmemberpravin parmar27-Jul-08 7:49 
Generalsimple questions Pinmembernewbie_to_csharp30-Jun-08 13:07 
GeneralA better solution for CLI (probabelly) PinmemberShlomiO21-May-08 22:33 
QuestionIsn't it time this was done by the controls themselves? Pinmembersimon heffer19-Oct-07 4:12 
GeneralNET Threading issues PinmemberCodeWatch5-Mar-07 19:25 
GeneralNice article Pinmember--=A J E E S H=--19-Feb-07 18:33 
GeneralAs some people mentioned... PinmemberItay Sagui19-Feb-07 11:51 
GeneralRe: As some people mentioned... PinmemberRobert Ensor21-Feb-07 15:30 
GeneralAnonymous Delegates Pinmembersirlantis19-Feb-07 9:13 
GeneralRe: Anonymous Delegates PinmemberOleg Shilo19-Feb-07 16:31 
GeneralRe: Anonymous Delegates Pinmemberdaniel.byrne20-Feb-07 1:44 
GeneralRe: Anonymous Delegates Pinmemberdaniel.byrne20-Feb-07 2:07 
GeneralRe: Anonymous Delegates PinmemberOleg Shilo20-Feb-07 3:33 
AnswerRe: Anonymous Delegates PinmemberRobert Ensor20-Feb-07 22:32 
GeneralRe: Anonymous Delegates PinmemberOleg Shilo21-Feb-07 15:23 
GeneralA similar method... PinmemberNeal Sanche6-Mar-07 19:06 
Generalsee SynchronizationContext Pinmember_SAM_19-Feb-07 6:30 
GeneralRe: see SynchronizationContext PinmemberPatrick Sears19-Feb-07 7:26 
GeneralHi guys, if you like this.. Pinmemberdaniel.byrne19-Feb-07 6:17 
GeneralRe: Hi guys, if you like this.. [modified] Pinmemberbombdrop19-Feb-07 6:30 
GeneralRe: Hi guys, if you like this.. Pinmemberdaniel.byrne19-Feb-07 7:20 
GeneralRe: Hi guys, if you like this.. Pinmemberdaniel.byrne19-Feb-07 7:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141216.1 | Last Updated 19 Feb 2007
Article Copyright 2007 by daniel.byrne
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid