views:

45

answers:

0

I've tried this MANY ways, here is the current iteration. I think I've just implemented this all wrong. What I'm trying to accomplish is to treat this Asynch result in such a way that until it returns AND I finish with my add-thumbnail call, I will not request another call to imageProvider.BeginGetImage.

To Clarify, my question is two-fold. Why does what I'm doing never seem to halt at my Mutex.WaitOne() call, and what is the proper way to handle this scenario?

        /// <summary>
        /// re-creates a list of thumbnails from a list of TreeElementViewModels (directories)
        /// </summary>
        /// <param name="list">the list of TreeElementViewModels to process</param>
        public void BeginLayout(List<AiTreeElementViewModel> list)
        {
            // *removed code for canceling and cleanup from previous calls*

            // Starts the processing of all folders in parallel.
            Task.Factory.StartNew(() =>
                {
                    thumbnailRequests = Parallel.ForEach<AiTreeElementViewModel>(list, options, ProcessFolder);
                });
        }

        /// <summary>
        /// Processes a folder for all of it's image paths and loads them from disk.
        /// </summary>
        /// <param name="element">the tree element to process</param>
        private void ProcessFolder(AiTreeElementViewModel element)
        {
            try
            {
                var images = ImageCrawler.GetImagePaths(element.Path);

                AsyncCallback callback = AddThumbnail;

                foreach (var image in images)
                {
                     Console.WriteLine("Attempting Enter");

                     synchMutex.WaitOne();     

                     Console.WriteLine("Entered");

                     var result = imageProvider.BeginGetImage(callback, image);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.ToString());
                // TODO: Do Something here.
            }
        }

        /// <summary>
        /// Adds a thumbnail to the Browser
        /// </summary>
        /// <param name="result">an async result used for retrieving state data from the load task.</param>
        private void AddThumbnail(IAsyncResult result)
        {
            lock (Thumbnails)
            {
                try
                {
                    Stream image = imageProvider.EndGetImage(result);
                    string filename = imageProvider.GetImageName(result);
                    string imagePath = imageProvider.GetImagePath(result);
                    var imageviewmodel = new AiImageThumbnailViewModel(image, filename, imagePath);
                    thumbnailHash[imagePath] = imageviewmodel;
                    HostInvoke(() => Thumbnails.Add(imageviewmodel));
                    UpdateChildZoom();
                    //synchMutex.ReleaseMutex();
                    Console.WriteLine("Exited");
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.ToString());
                    // TODO: Do Something here.
                }
            }
        }