Access HttpContext.Current from different threads

前端 未结 5 2069
轻奢々
轻奢々 2020-11-27 02:23

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

In HttpContext.Current.Session<

5条回答
  •  无人及你
    2020-11-27 03:08

    In my application there are a lot of code that uses HttpContext.Current and I can not modify that 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();
    

提交回复
热议问题