asp.net 2.0中實現非同步處理任務.

來源:互聯網
上載者:User

以下內容是在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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.