Click here to Skip to main content
12,747,154 members (29,336 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 27 Apr 2012

Avoiding InvokeRequired

, 27 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "C# Pivot Table"


This minor article is an enhancement to the excellent piece of work by Pablo Grisafi.  The goal is to enhance Pablo's code to work in cases where controls are not all created on the same (usually "UI") thread. 

Using the code  

The enhancement is relatively minor and virtually everyone with enough understanding of delegates and generics could easily enhance Pablo's version as I did below.  While Pablo's version works for 95+% cases, in very special circumstances, a control might need to be created on another thread, thus requiring InvokeRequired check on that control's property -- not on the form's.  The idea behind these enhancements is to allow the extension method to handle this type of scenario.  

The code below helps ensure that all controls are updated on the thread on which they were created even when they were created on different threads.  

static class ControlExtensions
  public static void InvokeOnOwnerThread<T>(this T control, Action<T> invoker) where T : Control
    if (control.InvokeRequired)
      control.Invoke(invoker, control);

The code that uses this extension method would look like this: 

private void UpdateFormTextFromSomeThread(string message)
  this.InvokeOnOwnerThread((form) => form.Text = message);  


No changes. 


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


About the Author

Igor Pashchuk, MBA
Software Developer (Senior) Credit Suisse
United States United States
I was introduced to computer programming at the age of 12 although I did not become particularly interested in software development until I turned 19 when I used one of my electives to take a computer science class. Since then, I never stopped writing software. My current interests are .NET, patterns and practices, and agile development (TDD and BDD).

You may also be interested in...

Comments and Discussions

QuestionConverting it to Pin
pilouk4-Oct-12 5:56
memberpilouk4-Oct-12 5:56 
AnswerRe: Converting it to Pin
Igor Pashchuk, MBA12-Oct-12 14:04
memberIgor Pashchuk, MBA12-Oct-12 14:04 
AnswerRe: Converting it to Pin
sage4530-Nov-12 7:33
membersage4530-Nov-12 7:33 
SuggestionInstantiating UI objects on a non-UI thread Pin
Member 34676551-Jun-12 11:09
memberMember 34676551-Jun-12 11:09 
GeneralRe: Instantiating UI objects on a non-UI thread Pin
Igor Pashchuk, MBA1-Jun-12 17:30
memberIgor Pashchuk, MBA1-Jun-12 17:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 27 Apr 2012
Article Copyright 2012 by Igor Pashchuk, MBA
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid