Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Windows Forms » Downloads
 
Add your own
alternative version

Avoiding InvokeRequired

, 28 Jun 2012 CPOL
How to avoid asking if InvokeRequired has the minimum code and no copy/paste
AvoidingInvokeRequired_src.zip
UIThread
UIThread
UIThread.gpState
UIThread.suo
UiThreadMobile
Properties
UiThreadMobile.csproj.user
Properties
Settings.settings
UIThreadVS2005_src.zip
UIThread2005
UIThread2005.suo
UIThread20CompactHelper
Properties
UIThread20CompactHelper.csproj.user
UIThread20CompactInheritance
Properties
UIThread20CompactInheritance.csproj.user
UIThread20DesktopHelper
Properties
Settings.settings
UIThread20DesktopInheritance
Properties
Settings.settings
UIThreadVS2008_src.zip
UIThread.gpState
UIThread.suo
UIThread20Compact
Properties
UIThread20Compact.csproj.user
UIThread20Desktop
Properties
Settings.settings
UIThread35Compact
Properties
UIThread35Compact.csproj.user
UIThread35Desktop
Properties
Settings.settings
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UIThread
{
    static public class FormExtensions
    {

        //Uncomment next line in windows mobile
        //public delegate void MethodInvoker(); 
        //because is not declared in CF!


        /// <summary>
        /// Runs code in UI thread synchronously.
        /// </summary>
        /// <param name="code">the code, like "delegate { this.Text = "new text"; }"
        /// </param>
        static public void UIThread(this Form form, MethodInvoker code)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(code);
                return;
            }
            code.Invoke();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

About the Author

Pablo Grisafi
Software Developer (Senior) TheWarrantyGroup
Argentina Argentina
I'm a Java/C# developer, with (some) experience in mobile and web development. I enjoy learning useful (and useless) stuff to be a better developer. I like to share the (little amount of) knowledge I have, by creating libraries and utility classes. CodeProject rocks! I wonder why the Java part is not so popular...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 28 Jun 2012
Article Copyright 2009 by Pablo Grisafi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid