Beispiel #1
0
        protected static void ParseLayerFile <T>(ParmObject <T> obj)
            where T : BaseObjectData
        {
            T data = obj.ParseFunc(obj.Path);

            AssignOwnerAndRegion(data, obj.PrefixOwnership, obj.PostfixOwnership, obj.Region);
            AssignCountry(data, obj.Country);

            // only lock on the same object can prevent concurrency
            // all ParmObject instances share the same objects
            lock (obj.Objects)
            {
                string key = data.Name.ToUpper();
                if (obj.Objects.ContainsKey(key))
                {
                    obj.Objects[key].MergeWith(data);
                }
                else
                {
                    obj.Objects[key] = data;
                }

                obj.Processed.Value++;
                if (obj.Processed.Value >= obj.Count)
                {
                    obj.Handle.Set();
                }
            }
        }
Beispiel #2
0
        public static List <T> Parse <T>(string[] layerPaths, string module, Dictionary <string, string> prefix,
                                         Dictionary <string, string> postfix, Dictionary <string, string> country,
                                         Dictionary <string, string> region, Func <string, T> parseFunc,
                                         string pattern = "*.xpo")
            where T : BaseObjectData
        {
            if (layerPaths == null || layerPaths.Length == 0)
            {
                return(new List <T>());
            }

            Dictionary <string, T> objects = new Dictionary <string, T>();
            IntRef processed = new IntRef(0);

            ManualResetEvent[] handles = new ManualResetEvent[] { new ManualResetEvent(false) };
            foreach (string layerPath in layerPaths)
            {
                FileInfo[] files = GetFiles(layerPath, module, pattern);
                if (files.Length == 0)
                {
                    continue;
                }

                processed.Value = 0;
                handles[0].Reset();
                foreach (FileInfo file in files)
                {
                    ParmObject <T> parm = new ParmObject <T>(files.Length, processed, file.FullName, parseFunc,
                                                             prefix, postfix, country, region, objects, handles[0]);
                    ThreadPool.QueueUserWorkItem(obj => ParseLayerFile <T>(obj as ParmObject <T>), parm);
                }
                WaitHandle.WaitAll(handles);
            }

            return(objects.Values.ToList());
        }