"Makarand Keer" <Ma**********@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
Hi All
I have problem in using Threading.
I have ASP.NET application in which I am using multithreading to start a
process.
Now the object instances which are used in this thread access
HttpContext.Current to get Session Variable value. Here my code fails.
Thread
on which the process is running does not have HTTPContext.Current object.
How can I pass-on HTTPContext.Current info to thread.
You can't and you shouldn't.
Your extra thread should not touch HttpContext or anything it refers to,
including the Page. This is because, as you've discovered (the hard way),
once the request is finished, the HttpContext and everything it refers to is
destroyed.
However, if your thread only needs data passed into it, and does not change
that data, then you can do the following:
1) Define a separate class called, for instance, MyThreadClass.
2) Define read-write properties for every value you want passed from the
page into the thread.
3) Define a private method in the class called, for instance, Run. This will
be used as your ThreadStart delegate.
4) Define a public method in the class called, for instance, StartThread.
This method will create the Thread and use "Thread.Start(new
ThreadStart(Run))" to start the thread.
5) When you want to start the thread, create an instance of MyThreadClass,
set the properties you need, then call StartThread.
This will work as long as you don't pass anything referenced by the
HttpContext. For instance, do not pass "Session". It's ok to pass something
which you've stored in Session state, but you can't pass Session itself (nor
Request, nor Response, etc.)
John Saunders