Beispiel #1
0
        private static void WorkerThreadProc(object unused)
        {
            Random rand = new Random();
            List <VeryLowProfileImageLoader.PendingRequest> list = new List <VeryLowProfileImageLoader.PendingRequest>();
            Queue <IAsyncResult> queue = new Queue <IAsyncResult>();

            VeryLowProfileImageLoader.RestoreState();
            while (!VeryLowProfileImageLoader._exiting)
            {
                object syncBlock = VeryLowProfileImageLoader._syncBlock;
                lock (syncBlock)
                {
                    int num = VeryLowProfileImageLoader.RemoveStaleAndGetCount();
                    if ((VeryLowProfileImageLoader._pendingRequests.Count == 0 && VeryLowProfileImageLoader._pendingResponses.Count == 0 && list.Count == 0 && queue.Count == 0) || num >= VeryLowProfileImageLoader.MaxSimultaneousLoads)
                    {
                        Monitor.Wait(VeryLowProfileImageLoader._syncBlock, 300);
                        if (VeryLowProfileImageLoader._exiting)
                        {
                            break;
                        }
                    }
                    while (0 < VeryLowProfileImageLoader._pendingRequests.Count)
                    {
                        VeryLowProfileImageLoader.PendingRequest pendingRequest = VeryLowProfileImageLoader._pendingRequests.Dequeue();
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i].Image == pendingRequest.Image)
                            {
                                list[i]        = pendingRequest;
                                pendingRequest = null;
                                break;
                            }
                        }
                        if (pendingRequest != null)
                        {
                            list.Add(pendingRequest);
                        }
                    }
                    while (0 < VeryLowProfileImageLoader._pendingResponses.Count)
                    {
                        queue.Enqueue(VeryLowProfileImageLoader._pendingResponses.Dequeue());
                    }
                }
                Queue <VeryLowProfileImageLoader.PendingCompletion> pendingCompletions = new Queue <VeryLowProfileImageLoader.PendingCompletion>();
                int num2 = 0;
                List <VeryLowProfileImageLoader.PendingRequest> list2 = new List <VeryLowProfileImageLoader.PendingRequest>();
                for (int j = 0; j < list.Count; j++)
                {
                    VeryLowProfileImageLoader.PendingRequest pendingRequest2 = list[j];
                    if (VeryLowProfileImageLoader._hashToUriDict[pendingRequest2.Image.GetHashCode()] != pendingRequest2.Uri)
                    {
                        list2.Add(pendingRequest2);
                    }
                }
                using (List <VeryLowProfileImageLoader.PendingRequest> .Enumerator enumerator = list2.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        VeryLowProfileImageLoader.PendingRequest current = enumerator.Current;
                        list.Remove(current);
                    }
                }
                if (list2.Count > 0)
                {
                    VeryLowProfileImageLoader.Log("Discarded " + list2.Count);
                }
                int num3 = VeryLowProfileImageLoader.RemoveStaleAndGetCount();
                IEnumerable <VeryLowProfileImageLoader.PendingRequest> arg_1F8_0 = list;
                Func <VeryLowProfileImageLoader.PendingRequest, bool>  arg_1F8_1 = new Func <VeryLowProfileImageLoader.PendingRequest, bool>((pr) => { return(VeryLowProfileImageLoader.HaveInDownloadedDict(pr)); });

                using (IEnumerator <VeryLowProfileImageLoader.PendingRequest> enumerator2 = Enumerable.Take <VeryLowProfileImageLoader.PendingRequest>(Enumerable.ToList <VeryLowProfileImageLoader.PendingRequest>(Enumerable.Where <VeryLowProfileImageLoader.PendingRequest>(arg_1F8_0, arg_1F8_1)), 3).GetEnumerator())
                {
                    while (enumerator2.MoveNext())
                    {
                        VeryLowProfileImageLoader.PendingRequest current2 = enumerator2.Current;
                        HttpWebRequest httpWebRequest = WebRequest.CreateHttp(current2.Uri);
                        httpWebRequest.AllowReadStreamBuffering = (true);
                        current2.DownloadStaredTimestamp        = DateTime.Now;
                        httpWebRequest.BeginGetResponse(new AsyncCallback(VeryLowProfileImageLoader.HandleGetResponseResult), new VeryLowProfileImageLoader.ResponseState(httpWebRequest, current2.Image, current2.Uri, current2.DownloadStaredTimestamp, current2.UniqueId, current2.CurrentAttempt));
                        list.Remove(current2);
                        VeryLowProfileImageLoader.Log("Processed Already downloaded " + current2.Uri);
                    }
                    goto IL_3DA;
                }
                //goto IL_2B9;
IL_3DA:
                if (num3 >= VeryLowProfileImageLoader.MaxSimultaneousLoads || list.Count <= 0)
                {
                    if (list.Count > 0)
                    {
                        syncBlock = VeryLowProfileImageLoader._syncBlock;
                        lock (syncBlock)
                        {
                            using (List <VeryLowProfileImageLoader.PendingRequest> .Enumerator enumerator = list.GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    VeryLowProfileImageLoader.PendingRequest current3 = enumerator.Current;
                                    VeryLowProfileImageLoader._pendingRequests.Enqueue(current3);
                                }
                            }
                        }
                    }
                    int num4 = 0;
                    while (0 < queue.Count && num4 < 5)
                    {
                        IAsyncResult asyncResult = queue.Dequeue();
                        VeryLowProfileImageLoader.ResponseState responseState = (VeryLowProfileImageLoader.ResponseState)asyncResult.AsyncState;
                        try
                        {
                            WebResponse webResponse = responseState.WebRequest.EndGetResponse(asyncResult);
                            pendingCompletions.Enqueue(new VeryLowProfileImageLoader.PendingCompletion(responseState.Image, responseState.Uri, webResponse.GetResponseStream(), responseState.Timestamp, responseState.RequestId));
                        }
                        catch (WebException e)
                        {
                            Logger.Instance.Error(string.Format("LowProfileImageLoader exception when fetching {0}", responseState.Uri.OriginalString), e);
                            if (responseState.CurrentAttempt <= 3)
                            {
                                Execute.ExecuteOnUIThread(delegate
                                {
                                    VeryLowProfileImageLoader.AddPendingRequest(responseState.Image, responseState.Uri, responseState.CurrentAttempt + 1);
                                });
                            }
                        }
                        Thread.Sleep(1);
                        num4++;
                    }
                    if (0 < pendingCompletions.Count)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(delegate
                        {
                            List <VeryLowProfileImageLoader.PendingCompletion> list3 = new List <VeryLowProfileImageLoader.PendingCompletion>();
                            while (0 < pendingCompletions.Count)
                            {
                                VeryLowProfileImageLoader.PendingCompletion pendingCompletion = pendingCompletions.Dequeue();
                                list3.Add(pendingCompletion);
                                VeryLowProfileImageLoader.AddToDownloadedDict(pendingCompletion.Uri);
                                VeryLowProfileImageLoader.AddToCache(pendingCompletion.Uri, pendingCompletion.Stream);
                                if (VeryLowProfileImageLoader.GetUriSource(pendingCompletion.Image) == pendingCompletion.Uri)
                                {
                                    BitmapImage bitmapImage = VeryLowProfileImageLoader.CreateSizedBitmap(pendingCompletion.Image, pendingCompletion.Uri, VeryLowProfileImageLoader.GetUseBackgroundCreation(pendingCompletion.Image));
                                    try
                                    {
                                        bitmapImage.SetSource(pendingCompletion.Stream);
                                    }
                                    catch (Exception e2)
                                    {
                                        Logger.Instance.Error("Error of reading image", e2);
                                    }
                                    pendingCompletion.Image.Source = (bitmapImage);
                                    DateTime arg_B8_0 = DateTime.Now;
                                    VeryLowProfileImageLoader._totalDownloadCount++;
                                    double totalMilliseconds = (arg_B8_0 - pendingCompletion.Timestamp).TotalMilliseconds;
                                    VeryLowProfileImageLoader.Log(string.Format("Downloaded image {0} in {1} ms. Totally downloaded = {2}", pendingCompletion.Uri.OriginalString, totalMilliseconds, VeryLowProfileImageLoader._totalDownloadCount));
                                    if (VeryLowProfileImageLoader._enableLog)
                                    {
                                        VeryLowProfileImageLoader._statistics.Add(totalMilliseconds);
                                        if (VeryLowProfileImageLoader._statistics.Count % 30 == 0 && VeryLowProfileImageLoader._statistics.Count > 0)
                                        {
                                            double num5 = Enumerable.Max(VeryLowProfileImageLoader._statistics);
                                            double num6 = Enumerable.Sum(VeryLowProfileImageLoader._statistics) / (double)VeryLowProfileImageLoader._statistics.Count;
                                            VeryLowProfileImageLoader.Log(string.Format("Statistics downloading: average {0}, max = {1}", num6, num5));
                                        }
                                    }
                                }
                                pendingCompletion.Stream.Dispose();
                            }
                            try
                            {
                                using (List <VeryLowProfileImageLoader.PendingCompletion> .Enumerator enumerator3 = list3.GetEnumerator())
                                {
                                    while (enumerator3.MoveNext())
                                    {
                                        VeryLowProfileImageLoader.RemoveRequestInProgress(enumerator3.Current.RequestId);
                                    }
                                }
                            }
                            catch (Exception)
                            {
                            }
                        });
                        continue;
                    }
                    continue;
                }
//		IL_2B9:
                if (num2 >= VeryLowProfileImageLoader.MaxSimultaneousLoads)
                {
                    VeryLowProfileImageLoader.Log("!!!!! noOfCycles=" + num2);
                }
                int count = list.Count;
                int indexOfElementWithMaxPriority = VeryLowProfileImageLoader.GetIndexOfElementWithMaxPriority(list, rand);
                VeryLowProfileImageLoader.PendingRequest pendingRequest3 = list[indexOfElementWithMaxPriority];
                list[indexOfElementWithMaxPriority] = list[count - 1];
                list.RemoveAt(count - 1);
                HttpWebRequest httpWebRequest2 = WebRequest.CreateHttp(pendingRequest3.Uri);
                httpWebRequest2.AllowReadStreamBuffering = (true);
                pendingRequest3.DownloadStaredTimestamp  = DateTime.Now;
                httpWebRequest2.BeginGetResponse(new AsyncCallback(VeryLowProfileImageLoader.HandleGetResponseResult), new VeryLowProfileImageLoader.ResponseState(httpWebRequest2, pendingRequest3.Image, pendingRequest3.Uri, pendingRequest3.DownloadStaredTimestamp, pendingRequest3.UniqueId, pendingRequest3.CurrentAttempt));
                VeryLowProfileImageLoader.AddRequestInProgress(pendingRequest3);
                num3++;
                VeryLowProfileImageLoader.Log(string.Concat(new object[]
                {
                    "Loading ",
                    pendingRequest3.Uri,
                    ";processing started in ",
                    (DateTime.Now - pendingRequest3.CreatedTimstamp).TotalMilliseconds,
                    " Currently loading ",
                    num3
                }));
                goto IL_3DA;
            }
        }
        private static void WorkerThreadProc(object unused)
        {
            Random rand = new Random();
            List <VeryLowProfileImageLoader.PendingRequest> pendingRequestList1 = new List <VeryLowProfileImageLoader.PendingRequest>();
            Queue <IAsyncResult> asyncResultQueue = new Queue <IAsyncResult>();

            VeryLowProfileImageLoader.RestoreState();
            while (!VeryLowProfileImageLoader._exiting)
            {
                lock (VeryLowProfileImageLoader._syncBlock)
                {
                    int local_9 = VeryLowProfileImageLoader.RemoveStaleAndGetCount();
                    if (VeryLowProfileImageLoader._pendingRequests.Count == 0 && VeryLowProfileImageLoader._pendingResponses.Count == 0 && (pendingRequestList1.Count == 0 && asyncResultQueue.Count == 0) || local_9 >= VeryLowProfileImageLoader.MaxSimultaneousLoads)
                    {
                        Monitor.Wait(VeryLowProfileImageLoader._syncBlock, 300);
                        if (VeryLowProfileImageLoader._exiting)
                        {
                            break;
                        }
                    }
                    while (0 < VeryLowProfileImageLoader._pendingRequests.Count)
                    {
                        VeryLowProfileImageLoader.PendingRequest local_10 = VeryLowProfileImageLoader._pendingRequests.Dequeue();
                        for (int local_11 = 0; local_11 < pendingRequestList1.Count; ++local_11)
                        {
                            if (pendingRequestList1[local_11].Image == local_10.Image)
                            {
                                pendingRequestList1[local_11] = local_10;
                                local_10 = null;
                                break;
                            }
                        }
                        if (local_10 != null)
                        {
                            pendingRequestList1.Add(local_10);
                        }
                    }
                    while (0 < VeryLowProfileImageLoader._pendingResponses.Count)
                    {
                        asyncResultQueue.Enqueue(VeryLowProfileImageLoader._pendingResponses.Dequeue());
                    }
                }
                Queue <VeryLowProfileImageLoader.PendingCompletion> pendingCompletions = new Queue <VeryLowProfileImageLoader.PendingCompletion>();
                int num1 = 0;
                List <VeryLowProfileImageLoader.PendingRequest> pendingRequestList2 = new List <VeryLowProfileImageLoader.PendingRequest>();
                for (int index = 0; index < pendingRequestList1.Count; ++index)
                {
                    VeryLowProfileImageLoader.PendingRequest pendingRequest = pendingRequestList1[index];
                    if (VeryLowProfileImageLoader._hashToUriDict[pendingRequest.Image.GetHashCode()] != pendingRequest.Uri)
                    {
                        pendingRequestList2.Add(pendingRequest);
                    }
                }
                foreach (VeryLowProfileImageLoader.PendingRequest pendingRequest in pendingRequestList2)
                {
                    pendingRequestList1.Remove(pendingRequest);
                }
                if (pendingRequestList2.Count > 0)
                {
                    VeryLowProfileImageLoader.Log("Discarded " + (object)pendingRequestList2.Count);
                }
                int count1 = VeryLowProfileImageLoader.RemoveStaleAndGetCount();
                foreach (VeryLowProfileImageLoader.PendingRequest pendingRequest in pendingRequestList1.Where <VeryLowProfileImageLoader.PendingRequest>((Func <VeryLowProfileImageLoader.PendingRequest, bool>)(pr => VeryLowProfileImageLoader.HaveInDownloadedDict(pr))).ToList <VeryLowProfileImageLoader.PendingRequest>().Take <VeryLowProfileImageLoader.PendingRequest>(3))
                {
                    HttpWebRequest http = WebRequest.CreateHttp(pendingRequest.Uri);
                    http.AllowReadStreamBuffering          = true;
                    pendingRequest.DownloadStaredTimestamp = DateTime.Now;
                    http.BeginGetResponse(new AsyncCallback(VeryLowProfileImageLoader.HandleGetResponseResult), (object)new VeryLowProfileImageLoader.ResponseState((WebRequest)http, pendingRequest.Image, pendingRequest.Uri, pendingRequest.DownloadStaredTimestamp, pendingRequest.UniqueId, pendingRequest.CurrentAttempt));
                    pendingRequestList1.Remove(pendingRequest);
                    VeryLowProfileImageLoader.Log("Processed Already downloaded " + (object)pendingRequest.Uri);
                }
                while (count1 < VeryLowProfileImageLoader.MaxSimultaneousLoads && pendingRequestList1.Count > 0)
                {
                    if (num1 >= VeryLowProfileImageLoader.MaxSimultaneousLoads)
                    {
                        VeryLowProfileImageLoader.Log("!!!!! noOfCycles=" + (object)num1);
                    }
                    int count2 = pendingRequestList1.Count;
                    int elementWithMaxPriority = VeryLowProfileImageLoader.GetIndexOfElementWithMaxPriority(pendingRequestList1, rand);
                    VeryLowProfileImageLoader.PendingRequest request = pendingRequestList1[elementWithMaxPriority];
                    pendingRequestList1[elementWithMaxPriority] = pendingRequestList1[count2 - 1];
                    pendingRequestList1.RemoveAt(count2 - 1);
                    HttpWebRequest http = WebRequest.CreateHttp(request.Uri);
                    http.AllowReadStreamBuffering   = true;
                    request.DownloadStaredTimestamp = DateTime.Now;
                    http.BeginGetResponse(new AsyncCallback(VeryLowProfileImageLoader.HandleGetResponseResult), (object)new VeryLowProfileImageLoader.ResponseState((WebRequest)http, request.Image, request.Uri, request.DownloadStaredTimestamp, request.UniqueId, request.CurrentAttempt));
                    VeryLowProfileImageLoader.AddRequestInProgress(request);
                    ++count1;
                    VeryLowProfileImageLoader.Log("Loading " + (object)request.Uri + ";processing started in " + (object)(DateTime.Now - request.CreatedTimstamp).TotalMilliseconds + " Currently loading " + (object)count1);
                }
                if (pendingRequestList1.Count > 0)
                {
                    lock (VeryLowProfileImageLoader._syncBlock)
                    {
                        foreach (VeryLowProfileImageLoader.PendingRequest item_1 in pendingRequestList1)
                        {
                            VeryLowProfileImageLoader._pendingRequests.Enqueue(item_1);
                        }
                    }
                }
                for (int index = 0; 0 < asyncResultQueue.Count && index < 5; ++index)
                {
                    IAsyncResult asyncResult = asyncResultQueue.Dequeue();
                    VeryLowProfileImageLoader.ResponseState responseState = (VeryLowProfileImageLoader.ResponseState)asyncResult.AsyncState;
                    try
                    {
                        WebResponse response = responseState.WebRequest.EndGetResponse(asyncResult);
                        pendingCompletions.Enqueue(new VeryLowProfileImageLoader.PendingCompletion(responseState.Image, responseState.Uri, response.GetResponseStream(), responseState.Timestamp, responseState.RequestId));
                    }
                    catch (WebException ex)
                    {
                        Logger.Instance.Error(string.Format("LowProfileImageLoader exception when fetching {0}", (object)responseState.Uri.OriginalString), (Exception)ex);
                        if (responseState.CurrentAttempt <= 3)
                        {
                            Execute.ExecuteOnUIThread((Action)(() => VeryLowProfileImageLoader.AddPendingRequest(responseState.Image, responseState.Uri, responseState.CurrentAttempt + 1)));
                        }
                    }
                    Thread.Sleep(1);
                }
                if (0 < pendingCompletions.Count)
                {
                    Deployment.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        List <VeryLowProfileImageLoader.PendingCompletion> pendingCompletionList = new List <VeryLowProfileImageLoader.PendingCompletion>();
                        while (0 < pendingCompletions.Count)
                        {
                            VeryLowProfileImageLoader.PendingCompletion pendingCompletion = pendingCompletions.Dequeue();
                            pendingCompletionList.Add(pendingCompletion);
                            VeryLowProfileImageLoader.AddToDownloadedDict(pendingCompletion.Uri);
                            VeryLowProfileImageLoader.AddToCache(pendingCompletion.Uri, pendingCompletion.Stream);
                            if (VeryLowProfileImageLoader.GetUriSource(pendingCompletion.Image) == pendingCompletion.Uri)
                            {
                                BitmapImage sizedBitmap = VeryLowProfileImageLoader.CreateSizedBitmap((FrameworkElement)pendingCompletion.Image, pendingCompletion.Uri, VeryLowProfileImageLoader.GetUseBackgroundCreation(pendingCompletion.Image));
                                try
                                {
                                    sizedBitmap.SetSource(pendingCompletion.Stream);
                                }
                                catch (Exception ex)
                                {
                                    Logger.Instance.Error("Error of reading image", ex);
                                }
                                pendingCompletion.Image.Source = (ImageSource)sizedBitmap;
                                DateTime now = DateTime.Now;
                                ++VeryLowProfileImageLoader._totalDownloadCount;
                                DateTime timestamp = pendingCompletion.Timestamp;
                                double totalMilliseconds = (now - timestamp).TotalMilliseconds;
                                VeryLowProfileImageLoader.Log(string.Format("Downloaded image {0} in {1} ms. Totally downloaded = {2}", (object)pendingCompletion.Uri.OriginalString, (object)totalMilliseconds, (object)VeryLowProfileImageLoader._totalDownloadCount));
                                if (VeryLowProfileImageLoader._enableLog)
                                {
                                    VeryLowProfileImageLoader._statistics.Add(totalMilliseconds);
                                    if (VeryLowProfileImageLoader._statistics.Count % 30 == 0 && VeryLowProfileImageLoader._statistics.Count > 0)
                                    {
                                        double num = VeryLowProfileImageLoader._statistics.Max();
                                        VeryLowProfileImageLoader.Log(string.Format("Statistics downloading: average {0}, max = {1}", (object)(VeryLowProfileImageLoader._statistics.Sum() / (double)VeryLowProfileImageLoader._statistics.Count), (object)num));
                                    }
                                }
                            }
                            pendingCompletion.Stream.Dispose();
                        }
                        try
                        {
                            foreach (VeryLowProfileImageLoader.PendingCompletion pendingCompletion in pendingCompletionList)
                            {
                                VeryLowProfileImageLoader.RemoveRequestInProgress(pendingCompletion.RequestId);
                            }
                        }
                        catch
                        {
                        }
                    }));
                }
            }
        }