public Retriever(System.Windows.Forms.Control owner) { if (owner == null) { throw new System.ArgumentNullException("System.Windows.Forms.Control owner"); } this.owner = owner; this.timeoutInterval = System.TimeSpan.FromSeconds(60); this.progressInterval = System.TimeSpan.FromSeconds(0.5); this.state = RetrieverState.NotReady; this.canceled = false; this.monitor = new System.Windows.Forms.Timer(); this.monitor.Tick += new System.EventHandler(this.monitorTick); }
// This function must be called by the client application to initiate retrieval. public void Start() { if (this.state == RetrieverState.NotReady) { throw new RetrieverStateException( "Retriever requires the URI to be set before calling Start()."); } if (this.state == RetrieverState.Retrieving) { throw new RetrieverStateException("Retrieval already in progress."); } try { this.startTime = System.DateTime.Now; this.previousProgressTime = this.startTime; this.webRequest = System.Net.WebRequest.Create(this.request.Uri); // Handle any proxy the system may have defined. System.Net.WebProxy proxy = System.Net.WebProxy.GetDefaultProxy(); if (proxy.Address != null) { this.webRequest.Proxy = proxy; } this.webRequest.BeginGetResponse( new System.AsyncCallback(this.onRetrieval), this); // Compute an interval to check on progress, but make sure it's // at least some reasonable minimum: say, 10 milliseconds. ulong monitorInterval = (ulong)System.Math.Min( this.timeoutInterval.Milliseconds, this.progressInterval.Milliseconds); this.monitor.Interval = (int)System.Math.Max(monitorInterval, 10); this.monitor.Start(); this.canceled = false; this.state = RetrieverState.Retrieving; } catch (System.Exception e) { throw e; } }
private void endRequest() { if (this.monitor.Enabled) { this.monitor.Stop(); } try { if (this.state == RetrieverState.Retrieving) { this.webRequest.Abort(); } } finally { this.state = RetrieverState.Done; } }