Exemplo n.º 1
0
        /// <summary>Процедура, обрабатывающая файлы из указанной директории в новом потоке</summary>
        /// <param name="_params">Передаваемые параметры</param>
        protected override void backgroundWorker(object _params)
        {
            MD5Hash md5Hash = new MD5Hash();
            string  hash    = "";
            List <MyTask.FileHashSumInfo> tempFileHashSumInfoSet = new List <MyTask.FileHashSumInfo>();
            List <string> tempFilesToProcess  = new List <string>();
            int           filesCountToProcess = 0;

            while ((filesCountToProcess = filesToProcess.Count) > 0 || !stopSignal)
            {
                if (filesCountToProcess > 0)
                {
                    lock (filesToProcess)
                    {
                        tempFilesToProcess = new List <string>(filesToProcess.AsReadOnly());
                        filesToProcess.Clear();
                    }

                    foreach (string file in tempFilesToProcess)
                    {
                        try
                        {
                            hash = md5Hash.fileHashSum(file).toString();

                            tempFileHashSumInfoSet.Add(new MyTask.FileHashSumInfo()
                            {
                                Path = file, Hash = hash
                            });
                            ProcessedFilesCount++;

                            if (portionProcFile == tempFileHashSumInfoSet.Count)
                            {
                                writeIntoDb(tempFileHashSumInfoSet);
                                tempFileHashSumInfoSet.Clear();
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            dBWriterHandle.writeLogInfo(ex.Message);
                        }
                    }
                }

                Thread.Sleep(150);
            }

            writeIntoDb(tempFileHashSumInfoSet);
        }
Exemplo n.º 2
0
        /// <summary>Метод, запускающий выполнение задачи: получение хеш-сумм файлов и запись результатов в БД</summary>
        public void run()
        {
            int currentTick = System.Environment.TickCount;

            Console.WriteLine("Запуск модуля получения хеш-сумм");
            hashSumFileCollectorHandle.start();
            Console.WriteLine("Запуск модуля сохранения результатов в БД");
            dbWwriterHandle.start();

            try
            {
                DirectoryHelper dirHelper = new DirectoryHelper(path);

                foreach (string[] files in dirHelper)
                {
                    hashSumFileCollectorHandle.addFileToProcess(files);
                }

                hashSumFileCollectorHandle.requestStop();
                hashSumFileCollectorHandle.waitForTask();
                Console.WriteLine("Все файлы и папки обработаны, всего файлов обработано: {0}, ожидаем завершения работы с БД", hashSumFileCollectorHandle.ProcessedFilesCount);

                dbWwriterHandle.requestStop();
                dbWwriterHandle.waitForTask();

                Console.WriteLine("Работа с БД закончена, вставлено строк {0}", dbWwriterHandle.InsertedFilesCount);
                Console.WriteLine("Время выполнения задания (мс): {0}", System.Environment.TickCount - currentTick);
            }
            catch (Exception ex)
            {
                dbWwriterHandle.writeLogInfo(ex.Message);
                Console.WriteLine(ex.Message);
            }

            dbHandle.Dispose();
        }