Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Changing a WinForms Control on the 'UI' Thread from another Thread

Rate me:
Please Sign up or sign in to vote.
4.81/5 (29 votes)
21 Aug 2010CPOL 61.7K   25   15
How to access a control from a different thread + good alternatives by other members!

Added: This article[^] has some interesting stuff about potential bugs with InvokeRequired - worth a read!


This comes up time and again.
Luc[^] has a great article explaining all here[^].


For those just wanting the solution...



C#
public void WithParameterMethod(int parameter)
{
    if (InvokeRequired)
        Invoke(new MethodInvoker(
            delegate
            {
                // Call your method again
                WithParameterMethod(parameter);
            }
            ));
    else
    {
        // Do your thing here!
    }
}


C#
public void NoParameterMethod()
{
    if (InvokeRequired)
        Invoke(new MethodInvoker(
            // Call your method again
            NoParameterMethod
            ));
    else
    {
        // Do your thing here!
    }
}

License

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


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Akram El Assas4-Apr-12 14:35
Akram El Assas4-Apr-12 14:35 
GeneralTo aspdotnetdev: Pin
DaveyM6923-Mar-10 16:49
professionalDaveyM6923-Mar-10 16:49 
GeneralRe: To aspdotnetdev: Pin
AspDotNetDev23-Mar-10 20:01
protectorAspDotNetDev23-Mar-10 20:01 
GeneralRe: To aspdotnetdev: Pin
Khaniya24-Mar-10 2:50
professionalKhaniya24-Mar-10 2:50 
GeneralRe: To aspdotnetdev: Pin
AspDotNetDev24-Mar-10 8:51
protectorAspDotNetDev24-Mar-10 8:51 
GeneralBeginInvoke Pin
Luc Pattyn23-Mar-10 15:39
sitebuilderLuc Pattyn23-Mar-10 15:39 
GeneralRe: BeginInvoke Pin
DaveyM6923-Mar-10 16:53
professionalDaveyM6923-Mar-10 16:53 
GeneralRe: BeginInvoke Pin
supercat924-Mar-10 4:24
supercat924-Mar-10 4:24 
GeneralWhy the down votes? [modified] Pin
DaveyM6923-Mar-10 0:30
professionalDaveyM6923-Mar-10 0:30 
GeneralRe: Why the down votes? Pin
HimanshuJoshi23-Mar-10 1:07
HimanshuJoshi23-Mar-10 1:07 
GeneralRe: Why the down votes? Pin
Sandeep Mewara23-Mar-10 2:39
mveSandeep Mewara23-Mar-10 2:39 
RantRe: Why the down votes? Pin
AWdrius23-Mar-10 22:41
AWdrius23-Mar-10 22:41 
GeneralRe: Why the down votes? Pin
DaveyM6923-Mar-10 23:31
professionalDaveyM6923-Mar-10 23:31 
GeneralRe: Why the down votes? Pin
AWdrius23-Mar-10 23:54
AWdrius23-Mar-10 23:54 
GeneralRe: Why the down votes? Pin
DaveyM6924-Mar-10 1:34
professionalDaveyM6924-Mar-10 1:34 

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.