Thursday, March 22, 2012

Thread Not Processing?

Hi all.
Hopefully someone will have run into this problem and will be able to
shine soem light on it for me.
Right. I have a class that is being called from an ASPX page which gets
all of its properties set and then calls a function which in effect
means it has to process quite a lot of files [essentially doing finds
on some xml documents and then spitting other files out].
As a stand alone method being called from ASP.NET there is no problem
at all, it runs perfectly, processing all the files and delivering the
right results so there are no problems with my class functionally for
what I'm trying to do.
The problem comes when we have thousands of files to process and the
whole thing slows down - aha says I, I'll run the method in a different
thread in the background, give the user a natty processing screen and
they will know what is going on.
for some reason I can't get the whole thing to process though.
I call the thread and kick off the process and that works fine, but in
the foreach loop I use to process all the files it just stalls after
the first process, seemingly waiting for something or the thread has
been suspended...
_filesProcessed++;
lock (System.Web.HttpContext.Current.Session) {
System.Web.HttpContext.Current.Session["ProcessCompleted"] = false;
System.Web.HttpContext.Current.Session["CurrentlyProcessing"] =
filename;
System.Web.HttpContext.Current.Session["PercentageComplete"] =
_filesProcessed.ToString();
}
Weirdly this is the bit it seems to get stuck on as absolutely
everything up to this point is executing. Could the lock be waiting for
the whole session object to become free? I had it executing without the
lock before and it did the same thing.
Any ideas why this won't work in a thread would be very much welcome.
Cheers
AndrewFHi AndrewF:

>_filesProcessed++;
>lock (System.Web.HttpContext.Current.Session) {
> System.Web.HttpContext.Current.Session["ProcessCompleted"] = false;
> System.Web.HttpContext.Current.Session["CurrentlyProcessing"] =
>filename;
> System.Web.HttpContext.Current.Session["PercentageComplete"] =
>_filesProcessed.ToString();
>}
>Weirdly this is the bit it seems to get stuck on as absolutely
>everything up to this point is executing. Could the lock be waiting for
>the whole session object to become free? I had it executing without the
>lock before and it did the same thing.
>
In a worker thread the HttpContext.Current property is going to be
null. The thread is not associated with a request. I'm 99% sure your
worker thread is now throwing a NullReferenceException and is silently
dying.
I have a description of the problem in the following article:
http://www.odetocode.com/Articles/112.aspx
You'll have to package up the data required by the worker thread and
pass it along so that the worker thread won't need to access a
HttpContext.
Scott
http://www.OdeToCode.com/blogs/scott/
Hi Scott,
Thanks for that... In the end this is what I had to do and pass data
back and forth that way. To be honest this will work better anyway as
it means the classes aren't specific to ASP.NET and I can use them in a
windows forms project as well.
Cheers for the explanation as well - really useful.
Cheers
AndrewF

0 comments:

Post a Comment