7

When a new async thread has been spawned using this signature, will the ASP.NET session object be available to the this new thread?

IAsyncResult asyncCall = f.BeginInvoke(null, f);
C. Ross
  • 31,137
  • 42
  • 147
  • 238
Narmatha Balasundaram
  • 867
  • 3
  • 11
  • 26
  • http://stackoverflow.com/questions/1382791/asp-net-what-to-do-if-current-session-is-null/1382811#1382811 – driis Feb 23 '11 at 20:42

1 Answers1

5

I don't know about which session object you are talking about but if you talk about the ASP.NET Session it might not be available. Also it is bad practice to access the ASP.NET Session from background threads. I would recommend you passing an object containing all the necessary information to this background tread instead of having it pull stuff from a session => makes it less reusable.

Normally if the caller of this thread waits for it to complete the session should be available all along but honestly it's bad design and I would simply avoid it.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yes, it is the ASP.NET session object I'm talking about. And it is because ASP.NET session object is not thread-safe? – Narmatha Balasundaram Feb 23 '11 at 20:41
  • 2
    And I would think that even if it's available when the thread starts, it could be disposed at any time, although if the caller is going to wait for the asynchronous task to complete before exiting, the session should remain through the task. – Jim Mischel Feb 23 '11 at 20:42
  • @Jim, good point, I've updated my answer to include this information. – Darin Dimitrov Feb 23 '11 at 20:44
  • @Darin So what is the best way to share objects between the background thread and the page? I'm facing the same issue. http://stackoverflow.com/questions/6152644/sharing-variables-between-an-asp-net-page-and-a-background-thread-created-by-the – NLV May 27 '11 at 13:55
  • @NLV, you could pass all the necessary information when starting the thread. As far as the thread passing information to the page is concerned, well, you will have to define what you mean by *page* as while the background thread executes the page that started this thread might be long gone. – Darin Dimitrov May 27 '11 at 14:03
  • I just don't want to pass and wait. I want to share. As the thread is getting executed I update the progress to the session variable which would be read from the page (when I poll every five seconds using asp.net timer control) and show the progress to the user. How about that? – NLV May 27 '11 at 14:13