Click here to Skip to main content
15,896,269 members
Articles / Desktop Programming / Windows Forms

Getting Around InvokeRequired Without Copy and Paste

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
17 Aug 20072 min read 121.9K   749   53  
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.
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using Castle.Core.Interceptor;
using Osherove.SimpleInterception;

namespace Osherove.SimpleInterception
{
    internal class MethodInterceptor : IInterceptor
    {
        private static Hashtable cache = new Hashtable();
        public void Intercept(IInvocation invocation)
        {
            if(invocation.Method.Name=="WndProcMessage")
            {
                invocation.Proceed();
                return;
            }

            AOPAttributeBase[] aops;
            string key = string.Format("{0}.{1}{2}", invocation.TargetType.FullName,invocation.Method.Name, GetArgumentNames(invocation));
            if (cache.ContainsKey(key))
            {
                aops = (AOPAttributeBase[]) cache[key];
            }
            else
            {
                aops = (AOPAttributeBase[]) invocation.Method.GetCustomAttributes(typeof (AOPAttributeBase), true);
                cache[key] = aops;
            }
            AOPInvocation invocationWrapper = new AOPInvocation(invocation);

            if (aops!=null)
            {
                foreach (AOPAttributeBase aop in aops)
                {
                    aop.OnInvoke(invocationWrapper);
                }
            }
            else
            {
                Console.WriteLine("no AOPs found");
            }
            if(!invocationWrapper.HasProceeded)
            {
                invocationWrapper.Proceed();
            }
        }


        private static string  GetArgumentNames(IInvocation invocation)
        {
            StringBuilder sb = new StringBuilder(50);
            foreach (object argument in invocation.Arguments)
            {
                sb.Append(argument.GetType().Name);
                sb.Append("|");
            }

            if (invocation.GenericArguments!=null)
            {
                foreach (Type argument in invocation.GenericArguments)
                {
                    sb.Append(argument.Name);
                    sb.Append("*");
                }
            }
            return sb.ToString();
        }
    }
}

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 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


Written By
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.

Comments and Discussions