在asp.net背景工人(线程池)(background worker(threadpool) in

2019-06-26 08:40发布

我有一个asp.net Web窗体其写的25-30项放入一个自定义缓存(当用户进行跟进从形式请求信息需要)。 目前,这一切都发生同步的主线程上。 但在高负荷addcache正在成为一个瓶颈。

我怎么能在后台运行这个任务,而不从asp.net工作进程的线程池耗时线程。

Answer 1:

备择方案:

完全异步:

  • 调用使用AJAX来自客户端的服务器代码,并将代码添加到监控呼叫的过程

    我只是说与此相关的进程答案:

    https://stackoverflow.com/a/11524718/1268570

部分异步:

从客户机到服务器的调用会同步,这意味着响应将不会返回到客户端,直到整个过程结束,但真正的代码将异步释放被ASP.Net提高可扩展性使用的线程中执行

  • 执行该页面异步。 您需要实现IHttpAsyncHandler界面在你的ASPX代码背后。 这是一个例子:

      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); } } 

    产量

  • 创建HttpAsyncHandler。 您需要创建一个自定义HttpHandler实现IHttpAsyncHandler接口。 例:

     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(); } } 


文章来源: background worker(threadpool) in asp.net