设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

在asp.net页面中使用异步读取

来源:网络收集 作者:佚名 时间:2007-04-25 23:26:28

刚亲自撰写一篇技术文章,请大家多指教。哈哈。
原文首发:http://bbs.5inet.net/topic.aspx?topicid=181

有的时候我们需要在网页里读取论坛的信息,在传统ASP的时候我们使用的是JS或者是IFRAME,这两种方式都不是很方便,而且对搜索引擎不友好。现在有了.Net,我们有了另一种方式。

要求:论坛需要提供RSS支持。

代码如下:


    #region task class
    //这是一个任务类,执行具体的任务
    public class RssAsyncTask
    {
        private String _rssContent;
        private AsyncTaskDelegate _dlgt;
        private string rssUrl;
        private bool _success;

        public bool IsSuccess
        {
            get
            {
                return _success;
            }
        }

        public RssAsyncTask(string rssUrl)
        {
            this.rssUrl = rssUrl;
        }

        // Create delegate.
        protected delegate void AsyncTaskDelegate();

        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)

[1] [2] [3]  下一页

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: