Exemplo n.º 1
0
        public async Task<bool> Analyze(ObservableCollection<CategoryListViewItem> categoryListViewItems, String deviceName, CancellationToken cancellationToken)
                {
                    //Delete the Temp Folder
                    Directory.Delete(Constants.TempDirectory, true);

                    PhotoInspector photoInspector = new PhotoInspector();

                    for(int categoryIndex = 0; categoryIndex < categoryListViewItems.Count; categoryIndex++)
                    {
                        Category category = new Category();
                        category.Name = categoryListViewItems[categoryIndex].Name;

                        //Expand the category
                        CategoryListViewItem newItem = new CategoryListViewItem();
                        newItem.Name = categoryListViewItems[categoryIndex].Name;
                        newItem.PhotoListViewItems = categoryListViewItems[categoryIndex].PhotoListViewItems;
                        newItem.IsExpanded = true;
                        categoryListViewItems[categoryIndex] = newItem;

                        //Using for loop instead of foreach because we edit the listview
                        for (int index = 0; index < categoryListViewItems[categoryIndex].PhotoListViewItems.Count; index++)
                        {
                            MarkPhotoAsBusy(categoryListViewItems[categoryIndex], index);

                            Task analysisTask = new Task(() =>
                            {
                                Photo photo = categoryListViewItems[categoryIndex].PhotoListViewItems[index].Photo;
                                string categoryName = categoryListViewItems[categoryIndex].Name;

                                //Check if the photo has been analyzed before
                                if (photo.PhotoDetails.Count == 0)
                                {
                                    photoInspector.InspectAndUpdatePhoto(photo, categoryName);
                                }
                            });

                            analysisTask.Start();
                            await analysisTask;

                            MarkPhotoAsCompleted(categoryListViewItems[categoryIndex], index);

                            if (cancellationToken.IsCancellationRequested)
                                break;
                        }

                        //Collapse the category
                        newItem = new CategoryListViewItem();
                        newItem.Name = categoryListViewItems[categoryIndex].Name;
                        newItem.PhotoListViewItems = categoryListViewItems[categoryIndex].PhotoListViewItems;
                        newItem.IsExpanded = false;
                        categoryListViewItems[categoryIndex] = newItem;
                    }

                    if (!cancellationToken.IsCancellationRequested)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
        public async Task <bool> Analyze(ObservableCollection <CategoryListViewItem> categoryListViewItems, String deviceName, CancellationToken cancellationToken)
        {
            //Delete the Temp Folder
            Directory.Delete(Constants.TempDirectory, true);

            PhotoInspector photoInspector = new PhotoInspector();

            for (int categoryIndex = 0; categoryIndex < categoryListViewItems.Count; categoryIndex++)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
                Category category = new Category();
                category.Name = categoryListViewItems[categoryIndex].Name;

                //Expand the category
                CategoryListViewItem newItem = new CategoryListViewItem();
                newItem.Name = categoryListViewItems[categoryIndex].Name;
                newItem.PhotoListViewItems           = categoryListViewItems[categoryIndex].PhotoListViewItems;
                newItem.IsExpanded                   = true;
                categoryListViewItems[categoryIndex] = newItem;

                //Using for loop instead of foreach because we edit the listview
                for (int index = 0; index < categoryListViewItems[categoryIndex].PhotoListViewItems.Count; index++)
                {
                    MarkPhotoAsBusy(categoryListViewItems[categoryIndex], index);

                    Task analysisTask = new Task(() =>
                    {
                        Photo photo         = categoryListViewItems[categoryIndex].PhotoListViewItems[index].Photo;
                        string categoryName = categoryListViewItems[categoryIndex].Name;

                        //Check if the photo has been analyzed before
                        if (photo.PhotoDetails.Count == 0)
                        {
                            photoInspector.InspectAndUpdatePhoto(photo, categoryName);
                        }
                    });

                    analysisTask.Start();
                    await analysisTask;

                    MarkPhotoAsCompleted(categoryListViewItems[categoryIndex], index);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                }

                //Collapse the category
                newItem      = new CategoryListViewItem();
                newItem.Name = categoryListViewItems[categoryIndex].Name;
                newItem.PhotoListViewItems           = categoryListViewItems[categoryIndex].PhotoListViewItems;
                newItem.IsExpanded                   = false;
                categoryListViewItems[categoryIndex] = newItem;

                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }
            }

            if (!cancellationToken.IsCancellationRequested)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }