多线程、HttpContext、长时间运行的任务?
我最近开始使用线程 Thread.Start()
和长时间运行的任务,并注意到几个与请求相关的问题。
编辑
您会怎么做建议在处理作业时向用户提供反馈,有效利用线程池并尽可能使用 HttpContext?
I got started with threads Thread.Start()
recently and Long Running Tasks and noticed several request related issues.
EDIT
What would you suggest to provide the user a feedback while the job is being processed, make efficient use of the thread pool and make use of HttpContext if possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您很可能在 ASP.NET 应用程序中使用线程时犯了一个错误。 您使用异步页面,还是使用 new Thread().Start? 后者是一个错误,当请求完成但线程仍在运行时,可能会让您陷入 HttpContext 的一些问题。
请编辑您的问题,以提供有关您使用线程执行的操作的更多详细信息。
You are quite possibly making a mistake in your use of threads in an ASP.NET application. Are you using asynchronous pages, or are you using new Thread().Start? The latter is a mistake, and can get you into some of those problems with HttpContext, when the request is complete, but the thread is still running.
Please edit your question to give more detail on what you're doing with threads.
对于访问数据:
对于像 Server.Identity.Name 这样的数据,我认为预先收集该信息并将其传递给异步代码是一个很好的方法。 它具有良好的解耦性,因为该代码现在仅取决于这几个属性。
对于访问行为:
您如何在 ASP.NET 中使用线程? 两种可行的方法是实现和注册 IAsyncHttpHandler,或者从某个 ASP.NET 页面调用 Page.AddOnPreRenderCompleteAsync()。
对于 IAsyncHttpHandler,您实现的回调将传递一个 HttpContext。 您应该能够从任何线程使用引用的上下文,直到您向 ASP.NET 指示请求处理已完成。 当然,您一次应该只使用一个线程中的该引用。
对于 Page.AddOnPreRenderCompleteAsync,在相同条件下从回调调用 Page.Context 应该是安全的。
对于 App_Code 中使用 HttpContext.Current 的现有代码,您需要对其进行重构,以便代码将 HttpContext 作为输入参数。 现有代码可以传入 HttpContext.Current,您从线程编写的新代码可以传入本答案前面描述的上下文之一。
For accessing data:
For data like Server.Identity.Name, I think collecting that information beforehand and passing it to your async code is a good approach. Its good decoupling as that code now only depends on those few properties.
For accessing behavior:
How are you using threads with ASP.NET? The two approaches that work are to implement and register IAsyncHttpHandler, or you're call Page.AddOnPreRenderCompleteAsync() from some ASP.NET page.
For IAsyncHttpHandler, the callback you implement is passed an HttpContext. You should be able to use that referenced context from any thread until you indicate to ASP.NET that request processing has finished. Of course you should only use that reference from one thread a time.
For Page.AddOnPreRenderCompleteAsync, it should be safe to call Page.Context from your callbacks under the same conditions.
For the existing code you have in App_Code that uses HttpContext.Current, you need to refactor that so the code takes the HttpContext as an input parameter. Existing code can pass in HttpContext.Current, new code you're writing from threads can pass in one of the contexts described earlier in this answer.
HttpContext 对象的 HttpContext.Current 属性是静态的。 并且可以在不同线程中或App_Code中的程序集中使用。 但是,它返回当前请求的 HttpContext 对象。
HttpContext 的所有静态属性都是线程安全的。 而且HttpContext的所有实例属性都不是线程安全的。
HttpContext.Current property of HttpContext object is static. And it can be used when in different threads or in the assemblies in the App_Code. But, it returns HttpContext object of the current Request.
All static properties of HttpContext are thread safe. And all the instance properties of HttpContext are not thread safe.