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

Extension method to update controls in a MultiThreaded WinForm application.

By , 18 Jun 2011
Rate this:
Please Sign up or sign in to vote.
This is the extension method:
 
       public static void ThreadSafeCall(this Control control, MethodInvoker method)
        {
            if (control.InvokeRequired)
            {
                try
                {
                    control.Invoke(method);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                method.Invoke();
            }
        }
 
Below is the use of it:
txtOutput.ThreadSafeCall((MethodInvoker)delegate { txtOutput.Text = "Updated"; });

License

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

About the Author

RakeshMeena
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralInteresting extension method, especially for simple things l... PinmemberRiz Thon20-Jun-11 16:18 
GeneralRe: Thanks Riz! PinmemberRakeshMeena20-Jun-11 18:43 

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 | Mobile
Web03 | 2.8.140415.2 | Last Updated 18 Jun 2011
Article Copyright 2011 by RakeshMeena
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid