Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

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

, 23 Mar 2010
Rate this:
Please Sign up or sign in to vote.
This version works regardless of parameters:public void AnyMethod(int parameter){ MethodInvoker wrapper = new MethodInvoker(delegate() { // Do your thing here! }); if (this.InvokeRequired) this.Invoke(wrapper); else wrapper();}Note also...
This version works regardless of parameters:
public void AnyMethod(int parameter)
{
    MethodInvoker wrapper = new MethodInvoker(delegate()
    {
        // Do your thing here!
    });
    if (this.InvokeRequired)
        this.Invoke(wrapper);
    else
        wrapper();
}
Note also that it does not need to be wrapped in a method to work. You can just place it inline. Any required variables will be captured by the delegate.
int parameter = 5;
MethodInvoker wrapper = new MethodInvoker(delegate()
{
    // Do your thing here! For example:
    lblCount.Text = parameter.ToString();
});
if (this.InvokeRequired)
    this.Invoke(wrapper);
else
    wrapper();

License

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

Share

About the Author

AspDotNetDev
Web Developer
United States United States
  • Managing Your JavaScript Library in ASP.NET (if you work with ASP.net and you don't read that, you are dead to me).
  • Graduated summa cum laude with a BS in Computer Science.
  • Wrote some articles and some tips.
  • DDR ("New high score? What does that mean? Did I break it?"), ping pong, and volleyball enthusiast.
  • Software I have donated to (you should too):

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 23 Mar 2010
Article Copyright 2010 by AspDotNetDev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid