示例#1
0
        private bool ProcessFile(string fileName, IUserConfiguration user, FilePathHelper filePathHelper)
        {
            int threadsUserCount = GetThreadCount(user.Name);

            if (threadsUserCount >= _configuration.MaxNumberOfThreads)
            {
                _logger.Debug($"There is no thread available for user {user.Name}. Is working with {threadsUserCount} threads.");
                return(false);
            }

            var processor = user.GetProcessor(_logger, _configuration, fileName);

            if (processor == null)
            {
                _logger.Error($"Error to process file:{fileName}. Can't find processor for file.");
                return(true);
            }

            string destFileName;

            if (!fileName.EndsWith(".retry"))
            {
                destFileName = $@"{filePathHelper.GetProcessedFilesFolder()}\{Path.GetFileName(fileName)}";
                File.Move(fileName, destFileName);
            }
            else
            {
                destFileName = fileName.Replace(".retry", ".processing");
                File.Move(fileName, destFileName);
            }

            _logger.Debug($"New thread for user:{user.Name}. Thread count:{threadsUserCount + 1}");

            var threadState = new ThreadStateInfo
            {
                FileName = destFileName,
                User     = user.Clone(),
                Handler  = new EventHandler <ThreadEventArgs>(ProcessFinishedHandler)
            };

            // Subscribe processor to stop event.
            ((FileCommandsWatcher)_watcher).StopSendEvent += processor.Processor_StopSendEvent;

            IncrementUserThreadCount(user.Name);
            ThreadPool.QueueUserWorkItem(new WaitCallback(processor.DoWork), threadState);

            // To mode debug.
            //processor.DoWork(threadState);

            return(true);
        }