Пример #1
0
        public IEnumerable <string> Process(string directoryName)
        {
            List <string> result = new List <string>();

            string[] files = GetFiles(directoryName);
            for (int i = 0; i < files.Length; i++)
            {
                if (Progress.IsCancellationPending())
                {
                    throw new UserTerminatedException();
                }

                Progress.SetMessage(1, MyConvert.Format("{0}/{1}, Processing {2}", i + 1, files.Length, files[i]));

                IFileProcessor processor = GetFileProcessor();
                if (processor is IThreadFileProcessor)
                {
                    (processor as IThreadFileProcessor).Progress = this.Progress;
                }

                result.AddRange(processor.Process(files[i]));
            }

            return(result);
        }
Пример #2
0
        public ItemInfoList ReadFromFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException(MyConvert.Format("File not found {0}", fileName));
            }

            return(ReadFromXml(XElement.Load(fileName, LoadOptions.SetBaseUri)));
        }
Пример #3
0
        public void TestCulture()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");

            var exp = "123.456";

            var d = Convert.ToDouble(exp);

            Assert.AreEqual(123456, d);

            d = MyConvert.ToDouble("123.456");
            Assert.AreEqual(123.456, d, 0.001);

            var ds = string.Format("{0:0.0000}", d);

            Assert.AreEqual("123,4560", ds);

            ds = MyConvert.Format("{0:0.0000}", d);
            Assert.AreEqual("123.4560", ds);
        }
Пример #4
0
        public override IEnumerable <string> Process()
        {
            PrepareBeforeProcessing();

            var result         = new ConcurrentBag <string>();
            var taskProcessors = GetTaskProcessors();

            if (ParallelMode && taskProcessors.Count > 1)
            {
                var exceptions = new ConcurrentQueue <Exception>();

                int totalCount = taskProcessors.Count;

                Progress.SetRange(0, totalCount);

                var finishedProcessors = new ConcurrentList <IParallelTaskProcessor>();
                var curProcessors      = new ConcurrentList <IParallelTaskProcessor>();

                Parallel.ForEach(taskProcessors, Option, (processor, loopState) =>
                {
                    curProcessors.Add(processor);

                    Progress.SetMessage("Processing {0}, finished {1} / {2}", curProcessors.Count, finishedProcessors.Count, totalCount);

                    processor.LoopState = loopState;
                    processor.Progress  = Progress;
                    try
                    {
                        var curResult = processor.Process();

                        foreach (var f in curResult)
                        {
                            result.Add(f);
                        }

                        curProcessors.Remove(processor);
                        finishedProcessors.Add(processor);

                        Progress.SetPosition(finishedProcessors.Count);
                        Progress.SetMessage("Processing {0}, finished {1} / {2}", curProcessors.Count, finishedProcessors.Count, totalCount);
                    }
                    catch (Exception e)
                    {
                        exceptions.Enqueue(e);
                        loopState.Stop();
                    }

                    GC.Collect();
                });

                if (Progress.IsCancellationPending())
                {
                    throw new UserTerminatedException();
                }

                if (exceptions.Count > 0)
                {
                    if (exceptions.Count == 1)
                    {
                        throw exceptions.First();
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (var ex in exceptions)
                        {
                            sb.AppendLine(ex.ToString());
                        }
                        throw new Exception(sb.ToString());
                    }
                }
            }
            else
            {
                for (int i = 0; i < taskProcessors.Count; i++)
                {
                    if (Progress.IsCancellationPending())
                    {
                        throw new UserTerminatedException();
                    }

                    string rootMsg = MyConvert.Format("{0} / {1}", i + 1, taskProcessors.Count);

                    Progress.SetMessage(1, rootMsg);
                    var processor = taskProcessors[i];
                    processor.Progress = Progress;

                    var curResult = processor.Process();

                    foreach (var f in curResult)
                    {
                        result.Add(f);
                    }
                }
            }

            DoAfterProcessing(result);

            return(result);
        }