Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C# 4.0

Extended Thread Pool

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
6 Apr 2013Ms-PL3 min read 81.9K   1.8K   119  
Your own extensible and configurable Thread Pool.
using System;
using System.Linq.Expressions;
using Nelibur.Sword.DataStructures;
using Nelibur.Sword.Logging;

namespace Nelibur.Sword.Extensions
{
    public static class ObjectExtensions
    {
        public static void BeginRaiseEvent<T>(this object sender, EventHandler<T> eventToRaise, T eventArgs)
            where T : EventArgs
        {
            if (eventToRaise == null)
            {
                return;
            }
            foreach (EventHandler<T> handler in eventToRaise.GetInvocationList())
            {
                EventHandler<T> handlerTemp = handler;
                handler.BeginInvoke(
                    sender,
                    eventArgs,
                    ar =>
                    {
                        try
                        {
                            handlerTemp.EndInvoke(ar);
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetLogger(typeof(ObjectExtensions)).Error(ex);
                        }
                    },
                    null);
            }
        }

        public static void BeginRaiseEvent<T>(
            this object sender, EventHandler<T> eventToRaise, Func<T> eventArgsCreator)
            where T : EventArgs
        {
            if (eventToRaise == null)
            {
                return;
            }
            foreach (EventHandler<T> handler in eventToRaise.GetInvocationList())
            {
                EventHandler<T> handlerTemp = handler;
                handler.BeginInvoke(
                    sender,
                    eventArgsCreator(),
                    ar =>
                    {
                        try
                        {
                            handlerTemp.EndInvoke(ar);
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetLogger(typeof(ObjectExtensions)).Error(ex);
                        }
                    },
                    null);
            }
        }

        public static string GetPropertyName<TValue, TResult>(
            this TValue obj, Expression<Func<TValue, TResult>> expression)
        {
            var member = expression.Body as MemberExpression;
            if (member != null)
            {
                return member.Member.Name;
            }
            throw new ArgumentException("Expression is not a member access", "expression");
        }

        public static bool IsNotNull(this object obj)
        {
            return obj != null;
        }

        public static bool IsNull(this object obj)
        {
            return obj == null;
        }

        public static void RaiseEvent<T>(this object sender, EventHandler<T> eventToRaise, T eventArgs)
            where T : EventArgs
        {
            if (eventToRaise == null)
            {
                return;
            }
            eventToRaise(sender, eventArgs);
        }

        public static void RaiseEvent<T>(this object sender, EventHandler<T> eventToRaise, Func<T> eventArgsCreator)
            where T : EventArgs
        {
            if (eventToRaise == null)
            {
                return;
            }
            eventToRaise(sender, eventArgsCreator());
        }

        public static Option<T> ToOption<T>(this T value)
        {
            if (typeof(T).IsValueType == false && ReferenceEquals(value, null))
            {
                return Option<T>.Empty;
            }
            return new Option<T>(value);
        }

        public static Option<TResult> ToType<TResult>(this object obj)
        {
            if (obj is TResult)
            {
                return new Option<TResult>((TResult)obj);
            }
            return Option<TResult>.Empty;
        }
    }
}

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 Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior)
United States United States
B.Sc. in Computer Science.

Comments and Discussions