private static void HandleGetResponseResult(IAsyncResult result)
        {
            var pendingResponse = result;

            PriorityQueue.AddWorkItem(() =>
            {
                var responseState = (ResponseState)pendingResponse.AsyncState;
                try
                {
                    var response = responseState.WebRequest.EndGetResponse(pendingResponse);

                    byte[] bytes = null;
                    using (var stream = response.GetResponseStream())
                    {
                        bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, (int)stream.Length);
                    }

                    var pc = new PendingCompletion(responseState.Image, responseState.Uri, new MemoryStream(bytes));
                    PriorityQueue.AddStorageWorkItem(() => _iso.Write(responseState.Uri, bytes));
                    PriorityQueue.AddUiWorkItem(() => HandleCompletion(pc));

                    //System.Diagnostics.Debug.WriteLine("Saving to the iso image cache...");
                }
                catch (WebException)
                {
                    // Ignore web exceptions (ex: not found)
                }
                catch
                {
                    // Other exceptions...
                }
            });
        }
        private static void ProcessTransfer(PendingRequest pendingRequest)
        {
            if (pendingRequest == null || pendingRequest.Uri == null)
            {
                return;
            }

            try
            {
                if (pendingRequest.Uri.IsAbsoluteUri)
                {
                    _iso.GetItem(pendingRequest.Uri, (img, exc, stat) =>
                    {
                        if (stat == IsoStoreCache.ItemCacheStatus.Hit)
                        {
                            var ms = new MemoryStream(img);
                            var pc = new PendingCompletion(pendingRequest.Image, pendingRequest.Uri, ms);
                            PriorityQueue.AddUiWorkItem(() =>
                            {
                                HandleCompletion(pc);
                            });
                        }
                        else
                        {
                            // Download from network
                            var webRequest = HttpWebRequest.CreateHttp(pendingRequest.Uri);
                            webRequest.AllowReadStreamBuffering = true;     // Don't want to block this thread or the UI thread on network access
                            webRequest.BeginGetResponse(HandleGetResponseResult, new ResponseState(webRequest, pendingRequest.Image, pendingRequest.Uri));
                        }
                    });
                }
                else
                {
                    // Load from application (must have "Build Action"="Content")
                    var originalUriString = pendingRequest.Uri.OriginalString;
                    // Trim leading '/' to avoid problems
                    var resourceStreamUri = originalUriString.StartsWith("/", StringComparison.Ordinal) ? new Uri(originalUriString.TrimStart('/'), UriKind.Relative) : pendingRequest.Uri;
                    // Enqueue resource stream for completion
                    var streamResourceInfo = Application.GetResourceStream(resourceStreamUri);
                    if (null != streamResourceInfo)
                    {
                        var pc = new PendingCompletion(pendingRequest.Image, pendingRequest.Uri, streamResourceInfo.Stream);
                        PriorityQueue.AddUiWorkItem(() =>
                        {
                            HandleCompletion(pc);
                        });
                    }
                }
            }
            catch (NullReferenceException)
            {
                // Trying to address user-found bugs here.
            }
        }
        private static PendingCompletion ProcessCompletion(IAsyncResult pendingResponse)
        {

            HttpWebResponse response = null;
            PendingCompletion completed;
            var responseState = (PrivateWebUserState)pendingResponse.AsyncState;

            try
            {
                response = (HttpWebResponse)responseState.Request.EndGetResponse(pendingResponse);
                var stream = response.GetResponseStream();
                completed = new PendingCompletion(responseState.Data, stream, null, response);
            }
            catch (Exception error)
            {
                if (response != null)
                    response.Close();

                completed = new PendingCompletion(responseState.Data, null, error, null);
            }

            return completed;
        }
        private void PrivateCompletionProcessor(PendingCompletion completion)
        {
            if (completion.Error != null)
            {
                WebServiceEventArgs response;

                if (completion.Error is WebException)
                {
                    response = GenerateFromException(completion.Error as WebException, HasInternetConnectivity, completion.RequestData.UserState);
                }
                else
                {
                    response = new WebServiceEventArgs(WebApiServerStatus.Error, completion.Error, String.Empty, completion.RequestData.UserState);
                }

                Responser(completion.RequestData, response);
            }
            else // Read data from stream and continue
            {
                WebServiceEventArgs finalResponse;

                using (var stream = completion.Result)
                {
                    using (var streamReader = new StreamReader(stream))
                    {
                        while (!streamReader.EndOfStream)
                        {
                            StringBuilder.Append(streamReader.ReadLine());
                        }

                        var stringResult = StringBuilder.ToString();
                        finalResponse = new WebServiceEventArgs(WebApiServerStatus.Success, null, stringResult, completion.RequestData.UserState);
                    }
                }

                completion.Response.Close();
                StringBuilder.Flush();

                Responser(completion.RequestData, finalResponse);
            }
        }
        private static void ProcessTransfer(PendingRequest pendingRequest)
        {
            if (pendingRequest == null || pendingRequest.Uri == null)
            {
                return;
            }

            try
            {
                if (pendingRequest.Uri.IsAbsoluteUri)
                {
                    _iso.GetItem(pendingRequest.Uri, (img, exc, stat) =>
                        {
                            if (stat == IsoStoreCache.ItemCacheStatus.Hit)
                            {
                                var ms = new MemoryStream(img);
                                var pc = new PendingCompletion(pendingRequest.Image, pendingRequest.Uri, ms);
                                PriorityQueue.AddUiWorkItem(() =>
                                {
                                    HandleCompletion(pc);
                                });
                            }
                            else
                            {
                                // Download from network
                                var webRequest = HttpWebRequest.CreateHttp(pendingRequest.Uri);
                                webRequest.AllowReadStreamBuffering = true; // Don't want to block this thread or the UI thread on network access
                                webRequest.BeginGetResponse(HandleGetResponseResult, new ResponseState(webRequest, pendingRequest.Image, pendingRequest.Uri));
                            }
                        });
                }
                else
                {
                    // Load from application (must have "Build Action"="Content")
                    var originalUriString = pendingRequest.Uri.OriginalString;
                    // Trim leading '/' to avoid problems
                    var resourceStreamUri = originalUriString.StartsWith("/", StringComparison.Ordinal) ? new Uri(originalUriString.TrimStart('/'), UriKind.Relative) : pendingRequest.Uri;
                    // Enqueue resource stream for completion
                    var streamResourceInfo = Application.GetResourceStream(resourceStreamUri);
                    if (null != streamResourceInfo)
                    {
                        var pc = new PendingCompletion(pendingRequest.Image, pendingRequest.Uri, streamResourceInfo.Stream);
                        PriorityQueue.AddUiWorkItem(() =>
                            {
                                HandleCompletion(pc);
                            });
                    }
                }
            }
            catch (NullReferenceException)
            {
                // Trying to address user-found bugs here.
            }
        }
        private static void HandleCompletion(PendingCompletion pendingCompletion)
        {
            // fadeClear, clearFade, clear, fadeRemoveSiblings are all specific
            // side effects that met my needs when building 4th & Mayor. At
            // this time I'm keeping these around.

            var bitmap = new BitmapImage();
            try
            {
                bitmap.SetSource(pendingCompletion.Stream);
                pendingCompletion.Image.Source = bitmap;
                var tag = pendingCompletion.Image.Tag as string;

                if (tag != null && tag == "clear")
                {
                    var img = pendingCompletion.Image;
                    ClearParentBackground(img);
                }
                else if (tag != null && (tag == "fadeClear" || tag == "clearFade"))
                {
                    var img = pendingCompletion.Image;
                    OpacityAnimator oa = null;
                    OpacityAnimator.EnsureAnimator(img, ref oa);
                    if (img != null)
                    {
                        img.Opacity = 0;
                        oa.GoTo(1.0, new Duration(TimeSpan.FromSeconds(1)), () =>
                            {
                                ClearParentBackground(img);
                                oa = null;
                            });
                    }
                }
                else if (tag != null && tag == "fadeRemoveSiblings")
                {
                    // Specialized for badge display.
                    var img = pendingCompletion.Image;
                    OpacityAnimator oa = null;
                    OpacityAnimator.EnsureAnimator(img, ref oa);
                    if (img != null)
                    {
                        img.Opacity = 0;
                        oa.GoTo(1.0, new Duration(TimeSpan.FromSeconds(1)), () =>
                        {
                            ClearNonImageSiblings(img);
                            oa = null;
                        });
                    }
                }
            }
            catch (Exception)
            {
                // Ignore image decode exceptions (ex: invalid image)
                // TODO: Consider what to do here.
                //QuietWatson.ReportException(ex);
            }
            finally
            {
                // Dispose of response stream
                if (pendingCompletion.Stream != null)
                {
                    pendingCompletion.Stream.Dispose();
                }
            }
        }
        private static void HandleGetResponseResult(IAsyncResult result)
        {
            var pendingResponse = result;
            PriorityQueue.AddWorkItem(() =>
                {
                    var responseState = (ResponseState)pendingResponse.AsyncState;
                    try
                    {
                        var response = responseState.WebRequest.EndGetResponse(pendingResponse);

                        byte[] bytes = null;
                        using (var stream = response.GetResponseStream())
                        {
                            bytes = new byte[stream.Length];
                            stream.Read(bytes, 0, (int)stream.Length);
                        }

                        var pc = new PendingCompletion(responseState.Image, responseState.Uri, new MemoryStream(bytes));
                        PriorityQueue.AddStorageWorkItem(() =>_iso.Write(responseState.Uri, bytes));
                        PriorityQueue.AddUiWorkItem(() => HandleCompletion(pc));

                        //System.Diagnostics.Debug.WriteLine("Saving to the iso image cache...");
                    }
                    catch (WebException)
                    {
                        // Ignore web exceptions (ex: not found)
                    }
                    catch
                    {
                        // Other exceptions...
                    }
                });
        }
        private static void HandleCompletion(PendingCompletion pendingCompletion)
        {
            // fadeClear, clearFade, clear, fadeRemoveSiblings are all specific
            // side effects that met my needs when building 4th & Mayor. At
            // this time I'm keeping these around.

            var bitmap = new BitmapImage();

            try
            {
                bitmap.SetSource(pendingCompletion.Stream);
                pendingCompletion.Image.Source = bitmap;
                var tag = pendingCompletion.Image.Tag as string;

                if (tag != null && tag == "clear")
                {
                    var img = pendingCompletion.Image;
                    ClearParentBackground(img);
                }
                else if (tag != null && (tag == "fadeClear" || tag == "clearFade"))
                {
                    var             img = pendingCompletion.Image;
                    OpacityAnimator oa  = null;
                    OpacityAnimator.EnsureAnimator(img, ref oa);
                    if (img != null)
                    {
                        img.Opacity = 0;
                        oa.GoTo(1.0, new Duration(TimeSpan.FromSeconds(1)), () =>
                        {
                            ClearParentBackground(img);
                            oa = null;
                        });
                    }
                }
                else if (tag != null && tag == "fadeRemoveSiblings")
                {
                    // Specialized for badge display.
                    var             img = pendingCompletion.Image;
                    OpacityAnimator oa  = null;
                    OpacityAnimator.EnsureAnimator(img, ref oa);
                    if (img != null)
                    {
                        img.Opacity = 0;
                        oa.GoTo(1.0, new Duration(TimeSpan.FromSeconds(1)), () =>
                        {
                            ClearNonImageSiblings(img);
                            oa = null;
                        });
                    }
                }
            }
            catch (Exception)
            {
                // Ignore image decode exceptions (ex: invalid image)
                // TODO: Consider what to do here.
                //QuietWatson.ReportException(ex);
            }
            finally
            {
                // Dispose of response stream
                if (pendingCompletion.Stream != null)
                {
                    pendingCompletion.Stream.Dispose();
                }
            }
        }