public String GetRssContent() { return _rssContent; } public void DoTheAsyncTask() { // Introduce an artificial delay to simulate a delayed // asynchronous task. Make this greater than the // AsyncTimeout property. WebClient wc = new WebClient(); try { _rssContent = wc.DownloadString(rssUrl); _success = true; } catch (Exception e) { _rssContent = e.Message; } finally { wc.Dispose(); } //Thread.Sleep(TimeSpan.FromSeconds(5.0)); }
// Define the method that will get called to // start the asynchronous task. public IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object extraData) { //_rssContent = "Beginning async task.";
_dlgt = new AsyncTaskDelegate(DoTheAsyncTask); IAsyncResult result = _dlgt.BeginInvoke(cb, extraData);
return result; }
// Define the method that will get called when // the asynchronous task is ended. public void OnEnd(IAsyncResult ar) { //_rssContent = "Asynchronous task completed."; _dlgt.EndInvoke(ar); }
// Define the method that will get called if the task // is not completed within the asynchronous timeout interval. public void OnTimeout(IAsyncResult ar) { _rssContent = "Ansynchronous task failed to complete " + "because it exceeded the AsyncTimeout parameter."; } } #endregion