private void ProcessRequests() { while (true) { // wait for an artwork request iPendingRequests.Wait(); // get the first request in the queue - leave the request in the queue for now so that if // the main thread requests it again, it will not get re-added and downloaded again string uri; ScalingUriConverter uriConverter; lock (iLock) { uri = iPendingRequests.FirstRequest; uriConverter = iUriConverter; } // download the image NSAutoreleasePool pool = new NSAutoreleasePool(); pool.Init(); UIImage image = null; try { string requestUri = uri; if (uriConverter != null) { requestUri = uriConverter.Convert(uri); } NSData data = NSData.FromUrl(NSUrl.FromString(requestUri)); image = ImageToFitSize(new UIImage(data), new CGSize(kDownscaleSize, kDownscaleSize)); } catch (Exception e) { UserLog.WriteLine("ArtworkCache.ProcessRequests: " + uri + " (" + e.Message + ")"); } pool.Dispose(); // insert the image into the cache List <Item> staleItems; lock (iLock) { // add to the cache Item item = new Item(uri, image); staleItems = iCacheData.Add(item); // remove the request iPendingRequests.Remove(uri); } // send notification that the image was added to the cache if (EventImageAdded != null) { EventImageAdded(this, new EventArgsArtwork(uri)); } // clean up all stale items outside of the lock foreach (Item item in staleItems) { if (item.Image != null) { item.Image.Dispose(); } } } }
private void ProcessRequests() { while (true) { // wait for an artwork request iPendingRequests.Wait(); // get the first request in the queue - leave the request in the queue for now so that if // the main thread requests it again, it will not get re-added and downloaded again string uri; ScalingUriConverter uriConverter; lock (iLock) { uri = iPendingRequests.FirstRequest; uriConverter = iUriConverter; } Trace.WriteLine(Trace.kKinskyDesktop, "ArtworkCache requesting " + uri); // download the image NSAutoreleasePool pool = new NSAutoreleasePool(); pool.Init(); NSImage image = null; try { string requestUri = uri; if (uriConverter != null) { requestUri = uriConverter.Convert(requestUri); } NSString s = NSString.StringWithUTF8String(Uri.UnescapeDataString(requestUri)); NSURL url = NSURL.URLWithString(s.StringByAddingPercentEscapesUsingEncoding(NSStringEncoding.NSUTF8StringEncoding)); image = new NSImage(url); } catch (Exception e) { UserLog.WriteLine("ArtworkCache.ProcessRequests: " + uri + " (" + e.Message + ")"); } pool.Release(); // insert the image into the cache List <Item> staleItems; lock (iLock) { // add to the cache Item item = new Item(uri, image); staleItems = iCacheData.Add(item); // remove the request iPendingRequests.Remove(uri); } // send notification that the image was added to the cache if (EventImageAdded != null) { EventImageAdded(this, new EventArgsArtwork(uri)); } // clean up all stale items outside of the lock foreach (Item item in staleItems) { if (item.Image != null) { item.Image.Release(); } } } }