public void DownloadStringAsync(Uri address, DownloadStringAsync_Completed callback) { CacheCalls++; // lookup address in cache Stream cachedResult = CacheLookup(address); if (cachedResult != null) { CacheHits++; CacheDownloadStringCompletedEventArgs eventArgs = new CacheDownloadStringCompletedEventArgs(new StreamReader(cachedResult), address); // Invoke on a different thread. Otherwise we make the callback from the same thread as the // original call and wierd things could happen. Thread thread = new Thread(() => callback(this, eventArgs)); thread.Start(); } else { CacheMisses++; // not found, request data HttpWebRequest requestGetter = (HttpWebRequest)HttpWebRequest.Create(address); requestGetter.BeginGetResponse( new AsyncCallback(new CacheCallback(this, callback, address).Callback), requestGetter); } }
public CacheCallback(HttpCache owner, DownloadStringAsync_Completed callback, Uri requestedAddress) { this.owner = owner; this.callback = callback; this.requestedAddress = requestedAddress; }