|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is more or less a reposting of Roy Osherove's posting here. This is a way to add an attribute, The only thing I have added is unit tests. These demonstrate that BackgroundThe 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 Using the Codepublic delegate void DoThreadedGoodManualType();
/// <summary>
/// This is good behavior, but this manual invoke required junk to be
/// done every time
/// </summary>
private void DoThreadedGoodManual()
{
if (this.InvokeRequired)
{
// Pass the same function to BeginInvoke, but the call would come on
// the correct thread and InvokeRequired will be false.
this.BeginInvoke(new DoThreadedGoodManualType(DoThreadedGoodManual));
return;
}
DoThreadedBad(); //now we can do our normal functionality with no worries
}
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 [RunInUIThread]
protected virtual void DoThreadedGoodAOP()
{
DoThreadedBad();
}
Ah, the luxury. Well, now you can! Points of InterestI had lots of problems trying to test the code because message pumping is done by And remember to check out Roy Osherove's blog. History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||