//public static async Task<ImageIndexerTransporter> IndexImagesAsync(ImageIndexerTransporter singleFolderToIndex) //{ // //create a task completion source // //the type of the result value must be the same // //as the type in the returning Task // TaskCompletionSource<ImageIndexerTransporter> tcs = new TaskCompletionSource<ImageIndexerTransporter>(); // await Task.Run(() => // { // List<ImageAtLocation> images = AssignmentIndexer.IndexImages(singleFolderToIndex); // singleFolderToIndex.SetImages(images); // tcs.SetResult(singleFolderToIndex); // }); // return tcs.Task; // //.ContinueWith(t => // //{ // //} // //); //} //public static List<ImageAtLocation> IndexImagesAsync(ImageIndexerTransporter folderToIndex, IO.SearchOption searchMode) //{ // // First make sure a folder for thumbnails images exist for this project // string projectThumbFolder = ImageHelper.GetProjectThumbnailsFolder(folderToIndex.ProjectId); // if (!IO.Directory.Exists(projectThumbFolder)) // IO.Directory.CreateDirectory(projectThumbFolder); // HashSet<string> unique = GetUniquePathNames(); // List<ImageAtLocation> images = IndexImagesInLocation(folderToIndex, searchMode); // List<ImageAtLocation> newImages = new List<ImageAtLocation>(); // foreach (ImageAtLocation img in images) // { // if (!unique.Contains(img.ImagePath)) // { // img.SaveToDatabase(); // newImages.Add(img); // } // } // return newImages; //} public static List <ImageAtLocation> IndexImages(ImageIndexerTransporter folderToIndex, IO.SearchOption searchMode) { // First make sure a folder for thumbnails images exist for this project string projectThumbFolder = ImageHelper.GetProjectThumbnailsFolder(folderToIndex.ProjectId); if (!IO.Directory.Exists(projectThumbFolder)) { IO.Directory.CreateDirectory(projectThumbFolder); } HashSet <string> unique = GetUniquePathNames(); List <ImageAtLocation> images = IndexImagesInLocation(folderToIndex, searchMode); List <ImageAtLocation> newImages = new List <ImageAtLocation>(); foreach (ImageAtLocation img in images) { if (!unique.Contains(img.ImagePath)) { img.SaveToDatabase(); newImages.Add(img); } } return(newImages); }
private static List <ImageAtLocation> IndexImagesInLocation(ImageIndexerTransporter singleFolderToIndex, IO.SearchOption searchMode) { List <ImageAtLocation> list = new List <ImageAtLocation>(); // Get all image files in directory and subdirectory if (IO.Directory.Exists(singleFolderToIndex.FolderPath)) { string[] images = IO.Directory.GetFiles(singleFolderToIndex.FolderPath, "*.jpg", searchMode); foreach (string path in images) { ImageAtLocation img = ProcessImage(path, singleFolderToIndex.ProjectId); if (img != null) { list.Add(img); } } } return(list); }
public async void StartIndexingAsync(ProjectModel model) { // Start a new background worker to index the folders in the project List <Task> tasks = new List <Task>(); foreach (ImageFoldersModel folderModel in model.ProjectFolders) { // Get all folders subdirectories SearchOption method = folderModel.IncludeSubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; List <string> allDirectories = FileHelper.GetDirectories(folderModel.FolderPath, searchOption: method); allDirectories.Add(folderModel.FolderPath); // Make sure we add the current folder to the query // Start async tasks for each subdirectory to index its contents foreach (string folder in allDirectories) { //ImageIndexerTransporter ps = new ImageIndexerTransporter(model.ProjectId, folder); //ImageIndexerTransporter modified = await this.AsyncIndexFolder(ps).ConfigureAwait(false); //int numAdded = await this.AsyncModifyCollections(modified).ConfigureAwait(false); ImageIndexerTransporter ps = new ImageIndexerTransporter(model.ProjectId, folder); tasks.Add(Task.Run(() => { List <ImageAtLocation> images = AssignmentIndexer.IndexImages(ps, SearchOption.TopDirectoryOnly); ps.SetImages(images); return(ps); }) .ContinueWith((r) => { ImageIndexerTransporter p = r.Result; StatusText += string.Format("Indexed folder {0}.\r\n", p.FolderPath); App.MapVM.ImageLocations.SetProjectId(p.ProjectId); // Extract valid and invalid locations List <ImageAtLocation> unknownLocations = new List <ImageAtLocation>(); List <ImageAtLocation> validLocation = AssignmentIndexer.ExtractValidLocations(p.Images, out unknownLocations); App.MapVM.ImageLocations.AddRange(validLocation); App.MapVM.UnassignedImageLocations.AddRange(unknownLocations); this.ApplyImageFilters(); // Write to time last indexed Dictionary <string, object> where = new Dictionary <string, object>(); where.Add(DAssignment.ProjectId, p.ProjectId); Dictionary <string, object> update = new Dictionary <string, object>(); update.Add(DAssignment.TimeLastIndexed, DateTime.Now.ToString(App.RegionalCulture)); int numAffected = App.DB.UpdateValue(DTables.Assignments, where, update); }//, System.Threading.CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.DenyChildAttach, TaskScheduler.Default )); //BackgroundWorker _worker = new BackgroundWorker(); //_worker.DoWork += (s, a) => //{ // ImageIndexerTransporter p = (ImageIndexerTransporter)a.Argument; // List<ImageAtLocation> images = AssignmentIndexer.IndexImages(p); // p.SetImages(images); // a.Result = p; //}; //_worker.RunWorkerCompleted += (s, a) => //{ // ImageIndexerTransporter p = (ImageIndexerTransporter)a.Result; // StatusText += string.Format("Indexed folder {0}.\r\n", p.FolderPath); // App.MapVM.ImageLocations.SetProjectId(p.ProjectId); // // Extract valid and invalid locations // List<ImageAtLocation> unknownLocations = new List<ImageAtLocation>(); // List<ImageAtLocation> validLocation = AssignmentIndexer.ExtractValidLocations(p.Images, out unknownLocations); // App.MapVM.ImageLocations.AddRange(validLocation); // App.MapVM.UnassignedImageLocations.AddRange(unknownLocations); // // Write to time last indexed // Dictionary<string, object> where = new Dictionary<string, object>(); // where.Add(DAssignment.ProjectId, p.ProjectId); // Dictionary<string, object> update = new Dictionary<string, object>(); // update.Add(DAssignment.TimeLastIndexed, DateTime.Now.ToString(App.RegionalCulture)); // int numAffected = App.DB.UpdateValue(DTables.Assignments, where, update); //}; //_worker.RunWorkerAsync(ps); } } await Task.WhenAll(tasks).ContinueWith(t => { // write your code here StatusText += string.Format("Completed indexing all folders.\r\n"); //App.MapVM.ImageLocations.TriggerCollectionChanged(true); this.ApplyImageFilters(); App.MapVM.FilterdImageLocations.TriggerCollectionChanged(true); App.MapVM.UnassignedImageLocations.TriggerCollectionChanged(true); }).ConfigureAwait(false); }
public void StartIndexBackgroundworker(ProjectModel model) { StatusText += string.Format("Started indexing all folders\r\n"); this.OverlayLoadMessage = "Please wait\r\nLoading images from the specified folders."; foreach (ImageFoldersModel folderModel in model.ProjectFolders) { if (Directory.Exists(folderModel.FolderPath)) { // Get all folders subdirectories SearchOption method = folderModel.IncludeSubfolders ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; ImageIndexerTransporter ps = new ImageIndexerTransporter(model.ProjectId, folderModel.FolderPath); BackgroundWorker _worker = new BackgroundWorker(); _worker.DoWork += (s, a) => { ImageIndexerTransporter p = (ImageIndexerTransporter)a.Argument; List <string> allDirectories = FileHelper.GetDirectories(ps.FolderPath, searchOption: method); allDirectories.Add(ps.FolderPath); // Make sure we add the current folder to the query // Start async tasks for each subdirectory to index its contents List <ImageAtLocation> images = new List <ImageAtLocation>(); foreach (string folder in allDirectories) { ImageIndexerTransporter transport = new ImageIndexerTransporter(p.ProjectId, folder); List <ImageAtLocation> tmp = AssignmentIndexer.IndexImages(transport, SearchOption.TopDirectoryOnly); images.AddRange(tmp); StatusText += string.Format("Indexed folder {0}.\r\n", folder); } p.SetImages(images); a.Result = p; }; _worker.RunWorkerCompleted += (s, a) => { ImageIndexerTransporter p = (ImageIndexerTransporter)a.Result; // Write to time last indexed Dictionary <string, object> where = new Dictionary <string, object>(); where.Add(DAssignment.ProjectId, p.ProjectId); Dictionary <string, object> update = new Dictionary <string, object>(); update.Add(DAssignment.TimeLastIndexed, DateTime.Now.ToString(App.RegionalCulture)); int numAffected = App.DB.UpdateValue(DTables.Assignments, where, update); StatusText += string.Format("Completed all subfolders of {0}.\r\n", p.FolderPath); StatusText += string.Format("To get an overview of all images without GPS coordinates, re-open this project."); App.MapVM.ImageLocations.SetProjectId(p.ProjectId); // Extract valid and invalid locations List <ImageAtLocation> unknownLocations = new List <ImageAtLocation>(); List <ImageAtLocation> validLocation = AssignmentIndexer.ExtractValidLocations(p.Images, out unknownLocations); App.MapVM.ImageLocations.AddRange(validLocation); //App.MapVM.UnassignedImageLocations.AddRange(unknownLocations); this.ApplyImageFilters(); this.OverlayLoadMessage = ""; }; _worker.RunWorkerAsync(ps); } } }