Ejemplo n.º 1
0
        /// <summary>
        /// 将数据传入到数据队列数组中
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="queue1"></param>
        /// <param name="queue2"></param>
        public static void JoinDataQueue(IXLWorksheet sheet, Queue <DataQueueInfo>[] dataQueues, int targetColumn, int threadCount)
        {
            //读取sheet
            int rows      = sheet.RangeUsed().RowCount();
            int dataCount = 0;

            for (int i = 3; i <= rows; i++)
            {
                int    no         = i - 2;
                string strKey     = sheet.Cell(i, 2).Value.ToString();
                string strChinese = sheet.Cell(i, 3).Value.ToString();
                string strEnglish = sheet.Cell(i, 4).Value.ToString();

                //获取真正的译文,确保不为空
                int row    = i;
                int column = targetColumn;

                string strTargetTranslation = sheet.Cell(i, targetColumn).Value.ToString();

                //检查是否为空,是抛出异常
                if (strTargetTranslation.Equals(""))
                {
                    //清空所有入队信息
                    for (int j = 0; j < dataQueues.Length; j++)
                    {
                        dataQueues[j].Clear();
                    }
                    throw new Exception();
                }
                //数据封装
                DataQueueInfo dataQueueInfo = new DataQueueInfo {
                    No               = no,
                    StrKey           = strKey,
                    Chinese          = strChinese,
                    English          = strEnglish,
                    TargtTranslation = strTargetTranslation,
                    Row              = row,
                    Column           = column
                };

                // 按序入队
                dataQueues[dataCount].Enqueue(dataQueueInfo);
                dataCount++;
                if (dataCount == Environment.ProcessorCount)
                {
                    dataCount = 0;
                }
            }
        }
        /// <summary>
        /// 线程处理过程
        /// </summary>
        /// <param name="queueInfo"></param>
        private void DealQueueData(Queue <DataQueueInfo> queueInfo)
        {
            while (queueInfo.Count > 0)
            {
                #region 出队,处理数据
                DataQueueInfo info = queueInfo.Dequeue();
                //UI方式中英宽度
                double chineseWidth = GetActualWidth(info.Chinese);
                double englishWidth = GetActualWidth(info.English);
                //UI方式标准宽度
                double stardandWidth = chineseWidth >= englishWidth ? chineseWidth : englishWidth;
                //方法方式中英宽度
                double chineseWidthByMethod = GetActualWidthByMethod(info.Chinese);
                double englishWidthByMethod = GetActualWidthByMethod(info.English);
                //方法方式标准宽度
                double stardandWidthByMethod = chineseWidthByMethod >= englishWidthByMethod ? chineseWidthByMethod : englishWidthByMethod;
                //基本字符串
                string strBase = chineseWidth >= englishWidth ? info.Chinese : info.English;

                //真正译文
                string strSimulation = info.TargtTranslation;

                //译文UI方式宽度
                double simulationWidth = GetActualWidth(strSimulation);
                //UI方式是否超长
                bool IsOverWidth = simulationWidth > stardandWidth ? true : false;
                //译文方法方式宽度
                double simulationWidthByMethod = GetActualWidthByMethod(strSimulation);
                //方法方式是否超长
                bool IsOverWidthByMethod = simulationWidthByMethod > stardandWidthByMethod ? true : false;
                #endregion

                //封装处理结果
                ResultQueueInfo resultQueueInfo = new ResultQueueInfo
                {
                    No      = info.No,
                    Key     = info.StrKey,
                    Chinese = info.Chinese,
                    English = info.English,
                    TranslationWidthOfControl = Math.Round(simulationWidth, 2),
                    StardandWidthOfControl    = Math.Round(stardandWidth, 2),
                    IsOverWidthOfControl      = IsOverWidth,
                    Simulation               = strSimulation,
                    StardandWidthOfMethod    = Math.Round(stardandWidthByMethod, 2),
                    TranslationWidthOfMethod = Math.Round(simulationWidthByMethod, 2),
                    IsOverWidthOfMethod      = IsOverWidthByMethod,
                    Row    = info.Row,
                    Column = info.Column
                };

                //获取超标准宽度数据
                if (IsOverWidthByMethod)
                {
                    dicOverWidthLocation.Add(info.Row, info.Column);
                }

                //添加到集合
                lock (obj)
                {
                    resultList.Add(resultQueueInfo);
                }

                //查询是否队列是否已经将数据处理完毕
                for (int i = 0; i < dataQueues.Length; i++)
                {
                    //发出该队列数据处理完毕信号
                    if (queueInfo.Equals(dataQueues[i]) && queueInfo.Count == 0)
                    {
                        autoResetEvent[i].Set();
                    }
                }
            }
        }