Exemplo n.º 1
0
        private void ForEachInThread <T>(object param)
        {
            ForEachParams <T> forEachParams = (ForEachParams <T>)param;

            try
            {
                while (!Canceling)
                {
                    T current;
                    lock (forEachParams.Source)
                    {
                        if (Canceling)
                        {
                            return;
                        }
                        if (forEachParams.Source.MoveNext())
                        {
                            current = forEachParams.Source.Current;
                            goto IL_009a;
                        }
                        if (!_enableInfiniteRepeat || _notImplementedReset)
                        {
                            return;
                        }
                        try
                        {
                            forEachParams.Source.Reset();
                        }
                        catch (NotImplementedException)
                        {
                            _notImplementedReset = true;
                            return;
                        }
                        OnRepeatCompleted(new MultiThreadingRepeatEventArgs(++_repeatCount));
                    }
                    continue;
IL_009a:
                    forEachParams.Action(current);
                }
            }
            catch (Exception)
            {
                Cancel();
            }
            finally
            {
                if (EndThread())
                {
                    forEachParams.Source.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        private void RunForEachOther <T>(IEnumerable <T> source, Action <T> action)
        {
            _endEnumerator = false;
            InitBeforeRun(_threadCount, needCreateBarrierForReps: false);
            ForEachParams <T> forEachParams = default(ForEachParams <T>);

            forEachParams.Action = action;
            forEachParams.Source = source.GetEnumerator();
            try
            {
                for (int i = 0; i < _threadCount; i++)
                {
                    StartThread(ForEachInThread <T>, forEachParams);
                }
            }
            catch (Exception)
            {
                EndWork();
                throw;
            }
        }