简体   繁体   中英

Access HttpContext.Current.Application from batch process c#

I have a C# ASP.NET web application which starts a thread running some methods in a class called SiteCrawler.cs .

In HttpContext.Current.Application I want to save some value 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.

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

I would recommend either passing your HttpContext as parameters when spawning your threads. Personally, I would remove your dependency on HttpContext and create an object whose job is to synchronize your threads (or store their results) and can be used to perform post processing after the parallel processing has completed.

HttpContext.Current.Application is a singleton, it will exist in all threads... Is this a web application? If it isn't you can always initialize the application yourself and assign it to HttpContext.Current.Application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM