Beispiel #1
0
        /// <summary>
        /// Creats two threads which will fill up our queue with urls to photos.
        /// </summary>
        private void CreatePhotoUrlsFetchers()
        {
            pictures_urls = new Queue <Uri>(number_of_photos);

            next_page_index  = number_of_photos / 2;                           // middle.
            next_page_index -= (next_page_index % number_of_pictures_in_page); // make sure we're a multiple of number_of_pictures_in_page.

            prev_page_index  = next_page_index;                                // BuildPicturesList method will remove number_of_pictures_in_page from prev_page_index as it starts.
            next_page_index -= number_of_pictures_in_page;                     // BuildPicturesList method will add number_of_pictures_in_page to next_page_index as it starts.

            mutex     = new Mutex();
            semaphore = new Semaphore(0, 600); // Should be enough.

            // Create two threads which will enter photos urls to queue.

            producer_one = new Thread(new ParameterizedThreadStart(BuildPicturesList));
            delegate_GetNexPrevPictureUrl next_method = new delegate_GetNexPrevPictureUrl(GetNextPicturesPageIndex);

            producer_one.Start(next_method);

            producer_two = new Thread(new ParameterizedThreadStart(BuildPicturesList));
            delegate_GetNexPrevPictureUrl prev_method = new delegate_GetNexPrevPictureUrl(GetPrevPicturesPageIndex);

            producer_two.Start(prev_method);
        }
Beispiel #2
0
        /// <summary>
        /// Adds picture URLs to queue, At the moment two threads are scanning the user's "Photos of You" album,
        /// both starts from the 'middle' of the album one advances forward the other backwards,
        /// this way the queue will fill up much quicker.
        /// </summary>
        /// <param name="next_prev_pic_page_url">Method which navigates to a photos page.</param>
        private void BuildPicturesList(object next_prev_pic_page_url)
        {
            try
            {
                // Casting given parameter.
                delegate_GetNexPrevPictureUrl getNexPrevPictureUrl = next_prev_pic_page_url as delegate_GetNexPrevPictureUrl;

                List <string> pictures_srcs;                               // list of picture URLs for the current page.
                int           current_page_index = getNexPrevPictureUrl(); // Starting position. we navigate both forward and backwards.

                do
                {
                    pictures_srcs = GetPicturedSrcs(current_page_index); // Get all photo URLs in current page.
                    if (pictures_srcs.Count == 0)
                    {
                        break;
                    }

                    mutex.WaitOne(); // TODD: check if queue is thread safe, if so mutex isn't needed.

                    foreach (string picture_src in pictures_srcs)
                    {
                        pictures_urls.Enqueue(new Uri(picture_src));
                    }

                    mutex.ReleaseMutex();

                    semaphore.Release(pictures_srcs.Count);                   // one for each image.

                    current_page_index = getNexPrevPictureUrl();              // Either move forward or backwards depending on the given next_prev_pic_page_url param.
                } while (pictures_srcs.Count > 0 && current_page_index >= 0); // index must be positive or equal to zero and we must have some pictures.
            }
            catch (ThreadAbortException ex)
            {
            }

            // We can logout once the queue is filled by both threads, Facebook doesn't check credentials when acssesing a photo directly.
            // But for the time being stay logged.
        }