4

How can i access my project sessions in background thread in my c#.net webapplication?

it gives me session value = null.

Any idea?

Amit Patel
  • 205
  • 1
  • 5
  • 15
  • Please state what you want to do this for. Normally, operations like this require synchronization - this gets you into the real of really hard programming. You may access the current session and store in a global var (a static class). – mozillanerd Oct 01 '11 at 06:24
  • I am having long running task to do as background, make sure I don't want to use windows service for that, as ajax.ajaxMethod(...) is not useful because through ajax we can not access controls on page, I have to use thread, but thread is having probs for project session variables. – Amit Patel Oct 01 '11 at 07:44

3 Answers3

4

Threads in the thread pool are managed by the system. These threads are not bound to the current request. Therefore, Session is not available for them.

session lost when multithreading though

rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
3

You have to be very careful about using background threads with ASP.NET. By the time the thread executes, the "current" request will probably be over. This means you can't access the page, or the request, or anything interesting.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
0

You can't access the session but you could share your data the same way, using: HttpRuntime.Cache

There are a few things to keep in mind though: unlike session, cache do expire. Also the cache is shared between all web users.

Johann
  • 12,158
  • 11
  • 62
  • 89