private static void OnUriSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            Image image = (Image)o;
            // ISSUE: explicit reference operation
            Uri newValue = (Uri)e.NewValue;

            if (newValue == null)
            {
                image.Source = (null);
                ImageViewerLowProfileImageLoader.Log("OnUriSourceChanged uri = NULL");
            }
            else
            {
                ImageViewerLowProfileImageLoader.Log(string.Format("OnUriSourceChanged uri = {0}", newValue));
                Stream cachedImageStream = ImageCache.Current.GetCachedImageStream(newValue.OriginalString);
                if (cachedImageStream != null)
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.CreateOptions = ((BitmapCreateOptions)18);
                    ((BitmapSource)bitmapImage).SetSource(cachedImageStream);
                    image.Source = ((ImageSource)bitmapImage);
                    // ISSUE: reference to a compiler-generated field
                    if (ImageViewerLowProfileImageLoader.ImageDownloaded == null)
                    {
                        return;
                    }
                    // ISSUE: reference to a compiler-generated field
                    ImageViewerLowProfileImageLoader.ImageDownloaded(null, new EventArgs());
                }
                else if (!newValue.IsAbsoluteUri || !ImageViewerLowProfileImageLoader.IsEnabled || DesignerProperties.IsInDesignTool)
                {
                    newValue.ToString();
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.CreateOptions = ((BitmapCreateOptions)18);
                    bitmapImage.UriSource     = newValue;
                    image.Source = ((ImageSource)bitmapImage);
                    // ISSUE: reference to a compiler-generated field
                    if (ImageViewerLowProfileImageLoader.ImageDownloaded == null)
                    {
                        return;
                    }
                    // ISSUE: reference to a compiler-generated field
                    ImageViewerLowProfileImageLoader.ImageDownloaded(null, new EventArgs());
                }
                else
                {
                    lock (ImageViewerLowProfileImageLoader._syncBlock)
                    {
                        ImageViewerLowProfileImageLoader._pendingRequests.Enqueue(new ImageViewerLowProfileImageLoader.PendingRequest(image, newValue));
                        Monitor.Pulse(ImageViewerLowProfileImageLoader._syncBlock);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void OnUriSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            Image image = (Image)o;
            Uri   uri   = (Uri)e.NewValue;

            if (uri == null)
            {
                image.Source = null;
                ImageViewerLowProfileImageLoader.Log("OnUriSourceChanged uri = NULL");
            }
            else
            {
                ImageViewerLowProfileImageLoader.Log(string.Format("OnUriSourceChanged uri = {0}", (object)uri));
                Stream cachedImageStream = ImageCache.Current.GetCachedImageStream(uri.OriginalString);
                if (cachedImageStream != null)
                {
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.CreateOptions = BitmapCreateOptions.DelayCreation | BitmapCreateOptions.BackgroundCreation;
                    bitmapImage.SetSource(cachedImageStream);
                    image.Source = (ImageSource)bitmapImage;
                    if (ImageViewerLowProfileImageLoader.ImageDownloaded == null)
                    {
                        return;
                    }
                    ImageViewerLowProfileImageLoader.ImageDownloaded(null, new EventArgs());
                }
                else if (!uri.IsAbsoluteUri || !ImageViewerLowProfileImageLoader.IsEnabled || DesignerProperties.IsInDesignTool)
                {
                    uri.ToString();
                    image.Source = (ImageSource) new BitmapImage()
                    {
                        CreateOptions = (BitmapCreateOptions.DelayCreation | BitmapCreateOptions.BackgroundCreation),
                        UriSource     = uri
                    };
                    if (ImageViewerLowProfileImageLoader.ImageDownloaded == null)
                    {
                        return;
                    }
                    ImageViewerLowProfileImageLoader.ImageDownloaded(null, new EventArgs());
                }
                else
                {
                    lock (ImageViewerLowProfileImageLoader._syncBlock)
                    {
                        ImageViewerLowProfileImageLoader._pendingRequests.Enqueue(new ImageViewerLowProfileImageLoader.PendingRequest(image, uri));
                        Monitor.Pulse(ImageViewerLowProfileImageLoader._syncBlock);
                    }
                }
            }
        }
        private static void WorkerThreadProc(object unused)
        {
            Random random = new Random();
            List <ImageViewerLowProfileImageLoader.PendingRequest> pendingRequestList = new List <ImageViewerLowProfileImageLoader.PendingRequest>();
            Queue <IAsyncResult> asyncResultQueue = new Queue <IAsyncResult>();

            while (!ImageViewerLowProfileImageLoader._exiting)
            {
                lock (ImageViewerLowProfileImageLoader._syncBlock)
                {
                    if (ImageViewerLowProfileImageLoader._pendingRequests.Count == 0 && ImageViewerLowProfileImageLoader._pendingResponses.Count == 0 && (pendingRequestList.Count == 0 && asyncResultQueue.Count == 0))
                    {
                        Monitor.Wait(ImageViewerLowProfileImageLoader._syncBlock);
                        if (ImageViewerLowProfileImageLoader._exiting)
                        {
                            break;
                        }
                    }
                    while (0 < ImageViewerLowProfileImageLoader._pendingRequests.Count)
                    {
                        ImageViewerLowProfileImageLoader.PendingRequest local_7 = ImageViewerLowProfileImageLoader._pendingRequests.Dequeue();
                        for (int local_8 = 0; local_8 < pendingRequestList.Count; ++local_8)
                        {
                            if (pendingRequestList[local_8].Image == local_7.Image)
                            {
                                pendingRequestList[local_8] = local_7;
                                local_7 = (ImageViewerLowProfileImageLoader.PendingRequest)null;
                                break;
                            }
                        }
                        if (local_7 != null)
                        {
                            pendingRequestList.Add(local_7);
                        }
                    }
                    while (0 < ImageViewerLowProfileImageLoader._pendingResponses.Count)
                    {
                        asyncResultQueue.Enqueue(ImageViewerLowProfileImageLoader._pendingResponses.Dequeue());
                    }
                }
                Queue <ImageViewerLowProfileImageLoader.PendingCompletion> pendingCompletions = new Queue <ImageViewerLowProfileImageLoader.PendingCompletion>();
                int count = pendingRequestList.Count;
                for (int index1 = 0; 0 < count && index1 < 5; ++index1)
                {
                    int index2 = random.Next(count);
                    ImageViewerLowProfileImageLoader.PendingRequest pendingRequest = pendingRequestList[index2];
                    pendingRequestList[index2] = pendingRequestList[count - 1];
                    pendingRequestList.RemoveAt(count - 1);
                    --count;
                    HttpWebRequest http = WebRequest.CreateHttp(pendingRequest.Uri);
                    http.AllowReadStreamBuffering = true;
                    http.BeginGetResponse(new AsyncCallback(ImageViewerLowProfileImageLoader.HandleGetResponseResult), (object)new ImageViewerLowProfileImageLoader.ResponseState((WebRequest)http, pendingRequest.Image, pendingRequest.Uri, pendingRequest.Timestamp));
                    Thread.Sleep(1);
                }
                for (int index = 0; 0 < asyncResultQueue.Count && index < 5; ++index)
                {
                    IAsyncResult asyncResult = asyncResultQueue.Dequeue();
                    ImageViewerLowProfileImageLoader.ResponseState responseState = (ImageViewerLowProfileImageLoader.ResponseState)asyncResult.AsyncState;
                    try
                    {
                        WebResponse response = responseState.WebRequest.EndGetResponse(asyncResult);
                        pendingCompletions.Enqueue(new ImageViewerLowProfileImageLoader.PendingCompletion(responseState.Image, responseState.Uri, response.GetResponseStream(), responseState.Timestamp));
                    }
                    catch (WebException ex)
                    {
                        Logger.Instance.Error(string.Format("LowProfileImageLoader exception when fetching {0}", (object)responseState.Uri.OriginalString), (Exception)ex);
                    }
                    Thread.Sleep(1);
                }
                if (0 < pendingCompletions.Count)
                {
                    Deployment.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        while (0 < pendingCompletions.Count)
                        {
                            ImageViewerLowProfileImageLoader.PendingCompletion pendingCompletion = pendingCompletions.Dequeue();
                            if (ImageViewerLowProfileImageLoader.GetUriSource(pendingCompletion.Image) == pendingCompletion.Uri)
                            {
                                BitmapImage bitmapImage;
                                if (pendingCompletion.Image.Source == null)
                                {
                                    bitmapImage = new BitmapImage()
                                    {
                                        CreateOptions = BitmapCreateOptions.BackgroundCreation
                                    }
                                }
                                ;
                                else
                                {
                                    bitmapImage = new BitmapImage();
                                }
                                try
                                {
                                    bitmapImage.SetSource(pendingCompletion.Stream);
                                }
                                catch (Exception ex)
                                {
                                    Logger.Instance.Error("Error of reading image", ex);
                                }
                                pendingCompletion.Image.Source = (ImageSource)bitmapImage;
                                DateTime now = DateTime.Now;
                                if (ImageViewerLowProfileImageLoader.ImageDownloaded != null)
                                {
                                    ImageViewerLowProfileImageLoader.ImageDownloaded(null, new EventArgs());
                                }
                                ImageViewerLowProfileImageLoader.Log(string.Format("Downloaded image {0} in {1} ms.", (object)pendingCompletion.Uri.OriginalString, (object)(now - pendingCompletion.Timestamp).TotalMilliseconds));
                            }
                            pendingCompletion.Stream.Dispose();
                        }
                    }));
                }
            }
        }