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

Tagged as

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

, 21 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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...

 
public void WithParameterMethod(int parameter)
{
    if (InvokeRequired)
        Invoke(new MethodInvoker(
            delegate
            {
                // Call your method again
                WithParameterMethod(parameter);
            }
            ));
    else
    {
        // Do your thing here!
    }
}
 
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)

Share

About the Author

DaveyM69
CEO Dave Meadowcroft
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAkram El Assas4-Apr-12 15:35 
GeneralTo aspdotnetdev: PinmvpDaveyM6923-Mar-10 17:49 
GeneralRe: To aspdotnetdev: Pinmemberaspdotnetdev23-Mar-10 21:01 
GeneralRe: To aspdotnetdev: PinmemberKhaniya24-Mar-10 3:50 
GeneralRe: To aspdotnetdev: Pinmemberaspdotnetdev24-Mar-10 9:51 
GeneralBeginInvoke PinmvpLuc Pattyn23-Mar-10 16:39 
GeneralRe: BeginInvoke PinmvpDaveyM6923-Mar-10 17:53 
GeneralRe: BeginInvoke Pinmembersupercat924-Mar-10 5:24 
GeneralWhy the down votes? [modified] PinmvpDaveyM6923-Mar-10 1:30 
GeneralRe: Why the down votes? PinmemberHimanshuJoshi23-Mar-10 2:07 
GeneralRe: Why the down votes? PinmemberSandeep Mewara23-Mar-10 3:39 
RantRe: Why the down votes? PinmemberAWdrius23-Mar-10 23:41 
GeneralRe: Why the down votes? PinmvpDaveyM6924-Mar-10 0:31 
GeneralRe: Why the down votes? PinmemberAWdrius24-Mar-10 0:54 
GeneralRe: Why the down votes? PinmvpDaveyM6924-Mar-10 2:34 

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
Web02 | 2.8.141220.1 | Last Updated 21 Aug 2010
Article Copyright 2010 by DaveyM69
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid