C#CPU多核任务处理

        public void WorkOnMultiCore(TArgument aArgument, System.Func<TArgument, long> aGetCount, System.Action aBeforeStart, System.Action<TArgument, IndexRange, IBackgroundThreadWorker<MultiCoreReport<TReport>>> aDoWork, System.Action<ProgressChangedEventArgs<TReport>, IBackgroundThreadWorker<MultiCoreReport<TReport>>> aProgressChanged, System.Action aWorkCompleted)
        {
            long tCount = aGetCount(aArgument);

            if (tCount == 0)
            {
                return;
            }

            int tThreads = System.Environment.ProcessorCount * 2;

            long tSize = tCount / tThreads;

            System.Collections.Generic.List<IndexRange> tIndexRanges = new System.Collections.Generic.List<IndexRange>();

            if (tSize == 0 || tCount <= 2 * tThreads)
            {
                tIndexRanges.Add(new IndexRange(0, tCount - 1));
            }
            else
            {
                long tLastValue = 0;
                for (int tThreadIndex = 0; tThreadIndex < tThreads - 1; tThreadIndex++)
                {
                    long tFromValue = tThreadIndex * tSize;

                    long tToValue = (tThreadIndex + 1) * tSize - 1;
                    tLastValue = tToValue;

                    IndexRange tIndexRange = new IndexRange(tFromValue, tToValue);

                    tIndexRanges.Add(tIndexRange);
                }

                // Add the last range index
                if (tLastValue + 1 < tCount)
                {
                    tIndexRanges.Add(new IndexRange(tLastValue + 1, tCount - 1));
                }
            }

            int tCounter = tIndexRanges.Count;

            this._Totals = new System.Collections.Generic.SortedList<System.Guid, double>();
            this._Currents = new System.Collections.Generic.SortedList<System.Guid, double>();
            this._AssertManualResetEvent = new System.Threading.ManualResetEvent(true);

            System.Threading.AutoResetEvent tSignal = new System.Threading.AutoResetEvent(false);

            foreach (IndexRange tIndexRange in tIndexRanges)
            {
                System.Guid tThreadGuid = System.Guid.NewGuid();

                MultiCoreArgument tMultiCoreArgument = new MultiCoreArgument(aArgument, tIndexRange, tThreadGuid);

                BackgroundThreadWorker<MultiCoreArgument, MultiCoreReport<TReport>, object> tBackgroundThreadWorker = BackgroundThreadWorker<MultiCoreArgument, MultiCoreReport<TReport>, object>.Prepare(
                    tMultiCoreArgument,
                    null,
                    (aArgumentForWorker, aWorker) =>
                    {
                        try
                        {
                            if (aDoWork != null)
                            {
                                MultiCoreBackgroundThreadInsiderWorker tMultiCoreBackgroundThreadWorker = new MultiCoreBackgroundThreadInsiderWorker(aArgumentForWorker.ThreadGuid, aWorker);
                                aDoWork(tMultiCoreArgument.Argument, tMultiCoreArgument.IndexRange, tMultiCoreBackgroundThreadWorker);
                            }

                            if (System.Threading.Interlocked.Decrement(ref tCounter) == 0)
                            {
                                tSignal.Set();
                            }
                        }
                        catch (System.Exception e)
                        {
                            Western.Objects.Common.Message(new Western.Common.SystemStackPosition(this.GetType(), "WorkOnMultiCore+DoWork"), new Western.Common.SystemStack(e));
                        }
                        return null;
                    },
                    (aProgress, aWorker) =>
                    {
                        if (aProgressChanged != null)
                        {
                            try
                            {
                                this._AssertManualResetEvent.WaitOne();
                                this._AssertManualResetEvent.Reset();

                                MultiCoreReport<TReport> tMultiCoreReport = aProgress.UserState;

                                this._Totals[tMultiCoreReport.ReportGuid] = aProgress.Total;
                                this._Currents[tMultiCoreReport.ReportGuid] = aProgress.Current;

                                double tTotal = 0;
                                double tCurrent = 0;

                                foreach (double tValue in this._Totals.Values)
                                {
                                    tTotal += tValue;
                                }

                                foreach (double tValue in this._Currents.Values)
                                {
                                    tCurrent += tValue;
                                }

                                this._AssertManualResetEvent.Set();

                                aProgressChanged(new ProgressChangedEventArgs<TReport>(tCurrent, tTotal, aProgress.Message, aProgress.UserState.Value), aWorker);
                            }
                            catch (System.Exception e)
                            {
                                Western.Objects.Common.Message(new Western.Common.SystemStackPosition(this.GetType(), "WorkOnMultiCore+Progress"), new Western.Common.SystemStack(e));
                            }
                        }
                    },
                    (aResult, aWorker) =>
                    {
                        if (aWorkCompleted != null)
                        {
                            aWorkCompleted();
                        }
                    }
                );

                this._BackgroundWorkers.Add(tThreadGuid, tBackgroundThreadWorker);
            }

            if (aBeforeStart != null)
            {
                aBeforeStart();
            }

            foreach (BackgroundThreadWorker<MultiCoreArgument, MultiCoreReport<TReport>, object> tBackgroundThreadWorker in this._BackgroundWorkers.Values)
            {
                System.Threading.ThreadPool.QueueUserWorkItem((aParameter) =>
                {
                    ((BackgroundThreadWorker<MultiCoreArgument, MultiCoreReport<TReport>, object>)aParameter).Start();

                }, tBackgroundThreadWorker);
            }

            tSignal.WaitOne();
        }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值