开发者

Problems doing asynch operations in C# using Mutex

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 开发者_JAVA百科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.
                }
            }
        }


To start with,

you create a Task to do a Parallel.ForEach to run a Method that Invokes a delegate.

Three levels of parallelism where 1 would be enough.

And if I read this right, inside the delegate you want to use a Mutex to run only 1 instance at a time.

Could you indicate which actions you want to happen in parallel?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜