访问HttpContext.Current来自不同线程 [英] Access HttpContext.Current from different threads

查看:253
本文介绍了访问HttpContext.Current来自不同线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有开始运行的一类叫做SiteCrawler.cs一些方法大约25不同的线程一个C#ASP.NET应用程序。

I have a C# ASP.NET application which starts about 25 different threads running some methods in a class called SiteCrawler.cs.

HttpContext.Current.Session 我要保存用户和present它向用户做出的搜索结果,当所有的线程都完成运行。我的问题是, HttpContext.Current 对象是在产生的线程空,因为它不存在那里。

In HttpContext.Current.Session I want to save the result of the search made by the user and present it to the user when all the threads are finished running. My problem is that the HttpContext.Current object is null in the spawned threads because it doesn't exist there.

什么选择我必须保存用户/会话特定的数据,而不当应用程序是多线程的使用,因为限制的会议?

What other options do I have to save user/session specific data without using session because of the limitations when the application is multithreaded?

我试图寻找的#1周围的每一寸找到一个解决方案,但没有任何运气....

I have tried to search around every inch of Stackoverflow to find a solution but without any luck....

推荐答案

在我的应用程序有很多code,它使用 HttpContext.Current 可以和我不修改code。

In my application there are a lot of code that uses HttpContext.Current and I can not modify that code.

worker.DoWork()从下面的示例使用了code。我只好在单独的线程中运行它。

worker.DoWork() from sample below uses that code. And I had to run it in separate thread.

我来到了以下解决方案:

I came to the following solution:

 HttpContext ctx = HttpContext.Current;
 Thread t = new Thread(new ThreadStart(() =>
                {
                    HttpContext.Current = ctx;
                    worker.DoWork();
                }));
 t.Start();
 // [... do other job ...]
 t.Join();

这篇关于访问HttpContext.Current来自不同线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆