public Texture11 GetTexture(string date, string url) { if (!cache.ContainsKey(date)) { Add(date, url); } WmsCahceEntry result = cache[date]; if (result.Texture != null) { return(result.Texture); } Texture11 tempTexture = null; foreach (WmsCahceEntry entry in cache.Values) { if ((entry.Date.CompareTo(date) > 0 && tempTexture != null)) { return(tempTexture); } if (entry.Texture != null) { tempTexture = entry.Texture; } } return(tempTexture); }
private void Add(string date, string url) { WmsCahceEntry entry = new WmsCahceEntry(date, url); cache.Add(date, entry); entry.Requested = true; // Do a background load on this ThreadPool.QueueUserWorkItem(new WaitCallback(LoadTexture), entry); }
public static void LoadTexture(object objEntry) { WmsCahceEntry entry = objEntry as WmsCahceEntry; entry.Requested = true; try { String dir = Properties.Settings.Default.CahceDirectory + "Data\\KmlCache\\"; if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // This is a expanded timeout version of WebClient MyWebClient Client = new MyWebClient(); string filename = dir + ((uint)entry.URL.GetHashCode32()).ToString() + ".png"; Stream stream = null; if (Uri.IsWellFormedUriString(entry.URL, UriKind.Absolute)) { byte[] data = Client.DownloadData(entry.URL); stream = new MemoryStream(data); } else { stream = File.Open(entry.URL, FileMode.Open); } FileStream fileStream = File.OpenWrite(filename); byte[] buffer = new byte[32768]; while (true) { int read = stream.Read(buffer, 0, buffer.Length); if (read <= 0) { break; } fileStream.Write(buffer, 0, read); } stream.Close(); fileStream.Close(); //todo this was loaded from the kmlicon API. Any Problems with that? entry.Texture = Texture11.FromFile(filename); entry.Loaded = true; } catch { entry.ErrorCount++; // retry until sucsess or MaxError Count exceeded if (entry.ErrorCount < MaxErrorCount) { entry.Requested = false; entry.Loaded = false; } else { entry.Loaded = true; } } finally { // mut.ReleaseMutex(); } }