background worker(threadpool) in asp.net

假如想象 提交于 2019-12-01 14:02:27
Jupaol

Alternatives:

Completely async:

Partially async:

The call from the client to the server will be sync which means the response won't be returned to the client until the whole process ends, but the real code will be executed async releasing the thread used by ASP.Net increasing scalability

  • Execute the page async. You need to implement the IHttpAsyncHandler interface in your ASPX code behind. This is an example:

        public partial class _Default : System.Web.UI.Page, IHttpAsyncHandler
        {
            public void EndProcessRequest(IAsyncResult result)
            {
                var context = (result as AsyncOperation).Context;
    
                context.Response.Write(string.Format("<p>End Process Request on {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
            }
    
            public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
            {
                var operation = new AsyncOperation(cb, this.Context, extraData);
                operation.StartAsync();
    
                this.Context.Response.Write(string.Format("<p>Begin Process Request on: {0}...</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
    
                return operation;
            }
        }
    
        public class AsyncOperation : IAsyncResult
        {
            private AsyncCallback asyncCallback;
    
            public AsyncOperation(AsyncCallback asyncCallback, HttpContext context, object state)
            {
                this.AsyncState = state;
                this.asyncCallback = asyncCallback;
                this.Context = context;
    
                this.IsCompleted = false;
                this.AsyncWaitHandle = null;
                this.CompletedSynchronously = false;
            }
    
            public HttpContext Context { get; private set; }
            public object AsyncState { get; private set; }
            public WaitHandle AsyncWaitHandle { get; private set; }
            public bool CompletedSynchronously { get; private set; }
            public bool IsCompleted { get; private set; }
    
            public void StartAsync()
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncOperation), this.AsyncState);
            }
    
            public void StartAsyncOperation(object workItemState)
            {
                // place here the async logic
    
                this.Context.Response.Write(string.Format("<p>Long Async operation started on: {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
                Thread.Sleep(2000);
                this.Context.Response.Write(string.Format("<p>Long Async operation ended on: {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
                this.IsCompleted = true;
                this.asyncCallback(this);
            }
        }
    

    Output

  • Create an HttpAsyncHandler. You need to create a custom HttpHandler implementing the IHttpAsyncHandler interface. Example:

    public class AsyncHandler : IHttpAsyncHandler
    {
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            var operation = new AsyncOperation(cb, context, extraData);
            operation.StartAsync();
    
            context.Response.Write(string.Format("<p>Begin Process Request on: {0}...</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
    
            return operation;
        }
    
        public void EndProcessRequest(IAsyncResult result)
        {
            var context = (result as AsyncOperation).Context;
    
            context.Response.Write(string.Format("<p>End Process Request on {0}</p>", Thread.CurrentThread.ManagedThreadId.ToString()));
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            throw new NotImplementedException();
        }
    }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!