asp.net 2.0中实现异步处理任务.

该博客记录了在newsgroups中解决问题所写的demo。介绍了PageAsyncTask在MSDN上的参考页面,说明在Page_Load方法中可同时启动4个较耗时任务并行运行,然后等待其运行完毕或超时,还给出了转载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下内容是在newsgroups中解决一个朋友的问题而写的demo, 留下给自己做个备忘录.

----

关于PageAsyncTask在MSDN上的参考
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.web/html/08ffac7a-2c3c-27ee-1445-5284a226be38.htm

说明: 此页面中, 在Page_Load方法中, 同时启动4个较耗时的任务(线程), 让它们并行的运行, 然后等待他们都运行完毕或超时.

using System;
using System.Diagnostics;
using System.Threading;
using System.Web.Mvc;
using System.Web.UI;
 
namespace Portal.Views.Home
{
    public partial class Index : ViewPage
    {
        private string _asyncResult;
        private AsyncTaskDelegate _asyncTask;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            Stopwatch sw = Stopwatch.StartNew();
 
            PageAsyncTask task1 =
                new PageAsyncTask(OnTaskBegin, OnTaskEnd, OnTaskTimeout, "A1", true);
            PageAsyncTask task2 =
                new PageAsyncTask(OnTaskBegin, OnTaskEnd, OnTaskTimeout, "B2", true);
            PageAsyncTask task3 =
                new PageAsyncTask(OnTaskBegin, OnTaskEnd, OnTaskTimeout, "C3", true);
            PageAsyncTask task4 =
                new PageAsyncTask(OnTaskBegin, OnTaskEnd, OnTaskTimeout, "D4", true);
 
            RegisterAsyncTask(task1);
            RegisterAsyncTask(task2);
            RegisterAsyncTask(task3);
            RegisterAsyncTask(task4);
 
            // By default, an asynchronous task will time out if it has not completed
            // within 45 seconds.
            AsyncTimeout = TimeSpan.FromSeconds(60);
 
            ExecuteRegisteredAsyncTasks();
 
            sw.Stop();
 
            // now, we can get the task result here.
            Response.Write(_asyncResult);
            Response.Write(sw.Elapsed.TotalSeconds);
        }
 
        private IAsyncResult OnTaskBegin(object sender,
                                         EventArgs e,
                                         AsyncCallback callback,
                                         object data)
        {
            _asyncTask = delegate(string text)
            {
                // TODO: add your async task here.
                string response =
                    string.Format("AsyncTask {0} started at: {1}. ", text, DateTime.Now);
                Thread.Sleep(TimeSpan.FromSeconds(10));
                response +=
                    string.Format("AsyncTask {0} ended at: {1}.\n", text, DateTime.Now);
                return response;
            };
 
            IAsyncResult result = _asyncTask.BeginInvoke((string) data, callback, data);
            return result;
        }
 
        private void OnTaskEnd(IAsyncResult ar)
        {
            _asyncResult += _asyncTask.EndInvoke(ar);
        }
 
        private void OnTaskTimeout(IAsyncResult ar)
        {
            _asyncResult = string.Format("AsyncTask {0} is time out.", ar.AsyncState);
        }
 
        #region Nested type: AsyncTaskDelegate
 
        protected delegate string AsyncTaskDelegate(string text);
 
        #endregion
    }
}

reference: http://msdn.microsoft.com/en-us/library/system.web.ui.pageasynctask(zh-cn).aspx

转载于:https://www.cnblogs.com/xzwplus/archive/2008/04/27/1172736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值