public MapViewModel(ProjectModel model) { BingMapsCredentialsProvider bingMapsCredentials = new BingMapsCredentialsProvider(); this.bingMapsCredentials = new ApplicationIdCredentialsProvider(bingMapsCredentials.Key); this._projectId = model.ProjectId; this._currentProject = model; this._imageLocations = new ImageAtLocationCollection(); this._unassignedImageLocations = new ImageAtLocationCollection(); // Set this to capture the change events this._filteredUnassignedImageLocations.CollectionChanged += _unassignedImageLocations_CollectionChanged; App.MapVM = this; // Need this as this is not set until after the constructor is complete // Create first load of content Tuple <int, int, int> data = AssignmentIndexer.LoadImagesFromDatabase(this._projectId); int numAvailableImages = data.Item1 + data.Item2; StatusText += string.Format("{0} Images loaded from database, where {1} are missing GPS information.\r\n", numAvailableImages, data.Item2); // Print error text that some images could not be found or is erased if (data.Item3 > 0) { StatusText += string.Format("{0} Images could no longer be found. They are either moved or erased.\r\n", data.Item3); } this.ApplyImageFilters(); if (numAvailableImages == 0) { //this.StartIndexBackgroundworker(model); //this.StartIndexingAsync(model); Task.Delay(800).ContinueWith( t => this.StartIndexBackgroundworker(model) ); } }
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); } } }