Esempio n. 1
0
        private static void EnqueueMissedFiles(IList <WatchElement> elements)
        {
            List <DateTime> fil = new List <DateTime>();

            lock (QueueHolder.SyncLock)
            {
                int i = 0;
                while (i < QueueHolder.Instance.Queue.Count)
                {
                    QueueItem qi = QueueHolder.Instance.Queue[i];
                    if (File.Exists(qi.FileFullPath))
                    {
                        fil.Add(File.GetLastWriteTime(qi.FileFullPath));
                        i++;
                    }
                    else
                    {
                        QueueHolder.Instance.Queue.Remove(qi);
                    }
                }
            }
            fil.Add(QueueHolder.Instance.LastActionTime);

            DateTime maxDate = fil.Max();

            foreach (WatchElement element in elements)
            {
                if (Directory.Exists(element.SourcePath))
                {
                    var filesNeedToEnqueue = Directory.GetFiles(element.SourcePath).Select(fileName => new { FileName = fileName, LastWriteTime = File.GetLastWriteTime(fileName) }).Where(file => file.LastWriteTime > maxDate);
                    foreach (var enqueuingFile in filesNeedToEnqueue)
                    {
                        NeedActionArgs args = new NeedActionArgs(enqueuingFile.FileName, element.SourcePath, element.FileNameParamRegExExpression);
                        NeedAction(null, args);
                    }
                }
            }
        }
Esempio n. 2
0
        private static void NeedAction(object sender, NeedActionArgs e)
        {
            if (File.Exists(e.FilePath))
            {
                string fileName = Path.GetFileNameWithoutExtension(e.FilePath);
                Dictionary <string, int>    errors     = new Dictionary <string, int>();
                List <string>               processed  = new List <string>();
                Dictionary <string, string> itemParams = new Dictionary <string, string>();
                try
                {
                    Regex           regex   = new Regex(e.NameExpression, RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Singleline);
                    string[]        groups  = regex.GetGroupNames();
                    MatchCollection matches = regex.Matches(fileName);
                    foreach (string group in groups)
                    {
                        string value = string.Empty;
                        foreach (Match match in matches)
                        {
                            Group matchGroup = match.Groups[group];
                            if (matchGroup.Success)
                            {
                                foreach (Capture capture in matchGroup.Captures)
                                {
                                    value += capture.Value;
                                }
                            }
                        }
                        itemParams.Add(group, value);
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(string.Format("Параметры элемента не были извлечены по причине ошибки: {0}", ex), Constants.TRACE_WARNING);
                }

                MethodInvoker addElement = (MethodInvoker) delegate
                {
                    if (!QueueHolder.Instance.Queue.Any(item => item.FileFullPath == e.FilePath))
                    {
                        QueueHolder.Instance.Queue.Add
                        (
                            new QueueItem()
                        {
                            FileFullPath     = e.FilePath,
                            FileExtension    = Path.GetExtension(e.FilePath),
                            FileName         = fileName,
                            Status           = ItemStatus.Queued,
                            Parameters       = itemParams,
                            Failures         = errors,
                            PassedProcessors = processed
                        }
                        );
                    }
                };

                lock (QueueHolder.SyncLock)
                {
                    if (_winMode)
                    {
                        _qwindow.Invoke(addElement);
                    }
                    else
                    {
                        addElement();
                    }
                }
            }
        }