Click here to Skip to main content
11,417,750 members (47,089 online)
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 Pin
Akram El Assas4-Apr-12 15:35
memberAkram El Assas4-Apr-12 15:35 
GeneralTo aspdotnetdev: Pin
DaveyM6923-Mar-10 17:49
mvpDaveyM6923-Mar-10 17:49 
GeneralRe: To aspdotnetdev: Pin
aspdotnetdev23-Mar-10 21:01
memberaspdotnetdev23-Mar-10 21:01 
GeneralRe: To aspdotnetdev: Pin
Khaniya24-Mar-10 3:50
memberKhaniya24-Mar-10 3:50 
GeneralRe: To aspdotnetdev: Pin
aspdotnetdev24-Mar-10 9:51
memberaspdotnetdev24-Mar-10 9:51 
GeneralBeginInvoke Pin
Luc Pattyn23-Mar-10 16:39
mvpLuc Pattyn23-Mar-10 16:39 
GeneralRe: BeginInvoke Pin
DaveyM6923-Mar-10 17:53
mvpDaveyM6923-Mar-10 17:53 
GeneralRe: BeginInvoke Pin
supercat924-Mar-10 5:24
membersupercat924-Mar-10 5:24 
GeneralWhy the down votes? [modified] Pin
DaveyM6923-Mar-10 1:30
mvpDaveyM6923-Mar-10 1:30 
GeneralRe: Why the down votes? Pin
HimanshuJoshi23-Mar-10 2:07
memberHimanshuJoshi23-Mar-10 2:07 
GeneralRe: Why the down votes? Pin
Sandeep Mewara23-Mar-10 3:39
memberSandeep Mewara23-Mar-10 3:39 
RantRe: Why the down votes? Pin
AWdrius23-Mar-10 23:41
memberAWdrius23-Mar-10 23:41 
GeneralRe: Why the down votes? Pin
DaveyM6924-Mar-10 0:31
mvpDaveyM6924-Mar-10 0:31 
GeneralRe: Why the down votes? Pin
AWdrius24-Mar-10 0:54
memberAWdrius24-Mar-10 0:54 
GeneralRe: Why the down votes? Pin
DaveyM6924-Mar-10 2:34
mvpDaveyM6924-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
Web01 | 2.8.150427.4 | Last Updated 21 Aug 2010
Article Copyright 2010 by DaveyM69
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid