public void IndexFiles(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null) { List <BhattacharyyaRecord> listOfRecords = new List <BhattacharyyaRecord>(); Double[,] normalizedHistogram = new double[16, 16]; int totalFileCount = imageFiles.Length; for (int i = 0; i < totalFileCount; i++) { var fi = imageFiles[i]; using (Image img = Image.FromFile(fi.FullName)) { normalizedHistogram = Bhattacharyya.CalculateNormalizedHistogram(img); } BhattacharyyaRecord record = new BhattacharyyaRecord { Id = i, ImageName = fi.Name, ImagePath = fi.FullName, NormalizedHistogram = MultiToSingle(normalizedHistogram) }; listOfRecords.Add(record); IndexBgWorker.ReportProgress(i); } BinaryAlgoRepository <List <BhattacharyyaRecord> > repo = new BinaryAlgoRepository <List <BhattacharyyaRecord> >(); repo.Save(listOfRecords); }
public void IndexFilesAsync(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null) { ConcurrentBag <BhattacharyyaRecord> listOfRecords = new ConcurrentBag <BhattacharyyaRecord>(); Double[,] normalizedHistogram = new double[16, 16]; int totalFileCount = imageFiles.Length; int i = 0; long nextSequence; //In the class scope: Object lockMe = new Object(); Parallel.ForEach(imageFiles, currentImageFile => { var fi = currentImageFile; using (Image img = Image.FromFile(fi.FullName)) { normalizedHistogram = Bhattacharyya.CalculateNormalizedHistogram(img); } lock (lockMe) { nextSequence = i++; } BhattacharyyaRecord record = new BhattacharyyaRecord { Id = nextSequence, ImageName = fi.Name, ImagePath = fi.FullName, NormalizedHistogram = MultiToSingle(normalizedHistogram) }; listOfRecords.Add(record); IndexBgWorker.ReportProgress(i); }); BinaryAlgoRepository <List <BhattacharyyaRecord> > repo = new BinaryAlgoRepository <List <BhattacharyyaRecord> >(); repo.Save(listOfRecords.ToList()); }