Skip to main content
Rollback to Revision 1
Source Link
Mast
  • 13.9k
  • 12
  • 57
  • 128

You are right! So what's about revision v2 ?

    public static void Foreach<T>(ICollection<T> source, Action<T> action)
    {
        var countdown = new CountdownEvent(source.Count);
        List<Exception> exceptions = null;
        foreach (var item in source)
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                var closure = (T)state;
                try
                {
                    action(closure);
                }
                catch (Exception ex)
                {
                    if (exceptions == null)
                        lock (countdown)
                        {
                            if (exceptions == null)
                                exceptions = new List<Exception>();
                        }

                    exceptions.Add(ex);
                }
                finally
                {
                    countdown.Signal();
                }
            }, item);
        }
        countdown.Wait();
        if (exceptions != null)
            throw new AggregateException(exceptions);
    }

You are right! So what's about revision v2 ?

    public static void Foreach<T>(ICollection<T> source, Action<T> action)
    {
        var countdown = new CountdownEvent(source.Count);
        List<Exception> exceptions = null;
        foreach (var item in source)
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                var closure = (T)state;
                try
                {
                    action(closure);
                }
                catch (Exception ex)
                {
                    if (exceptions == null)
                        lock (countdown)
                        {
                            if (exceptions == null)
                                exceptions = new List<Exception>();
                        }

                    exceptions.Add(ex);
                }
                finally
                {
                    countdown.Signal();
                }
            }, item);
        }
        countdown.Wait();
        if (exceptions != null)
            throw new AggregateException(exceptions);
    }
added 1279 characters in body
Source Link

You are right! So what's about revision v2 ?

    public static void Foreach<T>(ICollection<T> source, Action<T> action)
    {
        var countdown = new CountdownEvent(source.Count);
        List<Exception> exceptions = null;
        foreach (var item in source)
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                var closure = (T)state;
                try
                {
                    action(closure);
                }
                catch (Exception ex)
                {
                    if (exceptions == null)
                        lock (countdown)
                        {
                            if (exceptions == null)
                                exceptions = new List<Exception>();
                        }

                    exceptions.Add(ex);
                }
                finally
                {
                    countdown.Signal();
                }
            }, item);
        }
        countdown.Wait();
        if (exceptions != null)
            throw new AggregateException(exceptions);
    }

You are right! So what's about revision v2 ?

    public static void Foreach<T>(ICollection<T> source, Action<T> action)
    {
        var countdown = new CountdownEvent(source.Count);
        List<Exception> exceptions = null;
        foreach (var item in source)
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                var closure = (T)state;
                try
                {
                    action(closure);
                }
                catch (Exception ex)
                {
                    if (exceptions == null)
                        lock (countdown)
                        {
                            if (exceptions == null)
                                exceptions = new List<Exception>();
                        }

                    exceptions.Add(ex);
                }
                finally
                {
                    countdown.Signal();
                }
            }, item);
        }
        countdown.Wait();
        if (exceptions != null)
            throw new AggregateException(exceptions);
    }
Source Link

Multithreading extensions

I created a following class to manage multithreading without extra overhead, which exist when I use Parallel TPL class. It is also useful for systems without TPL.

Can it be enchanced?

    public static void Foreach<T>(this ICollection<T> source, Action<T> action)
    {
        var allDone = new ManualResetEventSlim(false);
        int completed = 0;
        foreach (var item in source)
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                var closure = (T)state;
                action(closure);
                if (Interlocked.Increment(ref completed) == source.Count)
                    allDone.Set();
            }, item);
        }
        allDone.Wait();
    }

    public static void Do(Action action1, Action action2)
    {
        var firstTask = QueueWaitableTask(action1);
        action2();
        firstTask.Wait();
    }

    public static ManualResetEventSlim QueueWaitableTask(Action action)
    {
        var result = new ManualResetEventSlim(false);
        ThreadPool.QueueUserWorkItem(mse =>
        {
            action();
            ((ManualResetEventSlim) mse).Set();
        }, result);
        return result;
    }