我有开始运行的一类叫做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
发布评论