Click here to Skip to main content
11,638,173 members (74,237 online)
Click here to Skip to main content

Getting Around InvokeRequired Without Copy and Paste

, 17 Aug 2007 74.7K 624 52
Rate this:
Please Sign up or sign in to vote.
Instead of copying and pasting the same if(InvokeRequired) logic in every multithreaded function, use attributes to make code cleaner, its centralize logic and make it self documenting.

Introduction

This is more or less a reposting of Roy Osherove's posting here. This is a way to add an attribute, [RunInUIThread], to every function with which you would normally use the if(InvokeRequired) pattern. I was looking for a way to not have to copy and paste the same code into all the functions in which I needed multi-threaded capabilities. I happened to come across Roy's article and thought it was the best thing since sliced bread. I think Microsoft should build this into the next .NET release as a standard language feature, so I'm trying to spread the word. Tell your friends, especially if your friends work at Microsoft.

The only thing I have added is unit tests. These demonstrate that InvokeRequired is needed, showing the manual solution and then the improved solution using the attributes. You'll need NUnit 2.4 to run the unit tests. Technology samples are fine, but tests give me a warm mushy feeling.

Background

The attached sample uses something called AOP, or aspect oriented programming. Without getting into all of the jargon associated with that, the simplest way to think of this example is that AOP gives us method interception capabilities. So, if we can put an attribute on a function, we can associate methods that can be fired before and after specific functions. In those functions, we can then check if we need to use InvokeRequired if the attribute has been specified, etc. The AOP parts of this example Roy got from the Castle Project. See links for more details.

Using the Code

public delegate void DoThreadedGoodManualType();
/// <span class="code-SummaryComment"><summary>
</span>

The generally accepted way of handling multi-threaded operation in WinForm applications is shown above. This works well, but the downside is that you are repeating the InvokeRequired code and also having to create delegates. What if, instead of the above, we could do this?

[RunInUIThread]
protected virtual void DoThreadedGoodAOP()
{
    DoThreadedBad();
}

Ah, the luxury. Well, now you can!

Points of Interest

I had lots of problems trying to test the code because message pumping is done by application.run in WinForms, which is needed for the InvokeRequried stuff to work. I tried in vain to get the tests going by just calling controls on separate threads, but alas, you need actual forms to see this in action. So, the unit test is somewhat unconventional in that it is actually launching a WinForm and the WinForm is raising an event, letting the test know that if InvokeRequired is true or false, then the WinForm immediately closes itself so it doesn't just sit there. If anyone can think of a better way, I'd like to see it!

And remember to check out Roy Osherove's blog.

History

  • 16-Aug-2007 - Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Paul B.
United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

You may also be interested in...

Comments and Discussions

 
GeneralNice Pin
CIDev25-May-11 7:08
memberCIDev25-May-11 7:08 
GeneralCopy and paste code? Who does that anymore :) Pin
Diamonddrake11-Nov-10 12:11
memberDiamonddrake11-Nov-10 12:11 
GeneralRe: Copy and paste code? Who does that anymore :) Pin
Paul B.11-Nov-10 12:30
memberPaul B.11-Nov-10 12:30 
GeneralRe: Copy and paste code? Who does that anymore :) Pin
Member 813403324-Oct-11 9:35
memberMember 813403324-Oct-11 9:35 
GeneralVery good Pin
fly9045-Apr-09 14:51
memberfly9045-Apr-09 14:51 
GeneralVery nice, but one comment Pin
Member 35976531-Apr-09 2:40
memberMember 35976531-Apr-09 2:40 
GeneralRe: Very nice, but one comment Pin
Member 35976531-Apr-09 2:55
memberMember 35976531-Apr-09 2:55 
GeneralRe: Very nice, but one comment Pin
Member 35976531-Apr-09 5:15
memberMember 35976531-Apr-09 5:15 
GeneralRe: Very nice, but one comment Pin
Paul B.1-Apr-09 5:47
memberPaul B.1-Apr-09 5:47 
GeneralExcellent Pin
manitra23-Jan-09 22:57
membermanitra23-Jan-09 22:57 
GeneralRe: Excellent Pin
Paul B.24-Jan-09 9:15
memberPaul B.24-Jan-09 9:15 
General:( Pin
mikker_12325-Aug-07 1:54
membermikker_12325-Aug-07 1:54 
GeneralRe: :( Pin
Paul B.25-Aug-07 5:04
memberPaul B.25-Aug-07 5:04 
GeneralNice but... Pin
Igor Velikorossov19-Aug-07 14:10
memberIgor Velikorossov19-Aug-07 14:10 
GeneralRe: Nice but... Pin
Paul B.19-Aug-07 15:33
memberPaul B.19-Aug-07 15:33 
GeneralRe: Nice but... Pin
Igor Velikorossov19-Aug-07 15:41
memberIgor Velikorossov19-Aug-07 15:41 
GeneralRe: Nice but... Pin
Paul B.20-Aug-07 3:22
memberPaul B.20-Aug-07 3:22 
GeneralRe: Nice but... Pin
Paul B.20-Aug-07 5:21
memberPaul B.20-Aug-07 5:21 
GeneralAwesome and awesome Pin
reinux19-Aug-07 10:29
memberreinux19-Aug-07 10:29 
GeneralRe: Awesome and awesome Pin
Paul B.19-Aug-07 10:38
memberPaul B.19-Aug-07 10:38 
GeneralEeeexcellent Pin
Guido_d18-Aug-07 2:44
memberGuido_d18-Aug-07 2:44 
GeneralParameters Pin
Roguer117-Aug-07 21:44
memberRoguer117-Aug-07 21:44 
GeneralRe: Parameters Pin
Paul B.18-Aug-07 2:45
memberPaul B.18-Aug-07 2:45 
That's what's great, parameters are fine. I don't have any parameters to the functions in this example but you don't have to worry about that.
GeneralRe: Parameters Pin
Paul B.18-Aug-07 2:48
memberPaul B.18-Aug-07 2:48 

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
Web02 | 2.8.150728.1 | Last Updated 17 Aug 2007
Article Copyright 2007 by Paul B.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid