views:

60

answers:

3

I'm currently using ThreadPool.QueueUserWorkItem in an Asp.Net application. Basically a user uploads a file using a form with an FileUpload Control. The file can take quite a while to process which was causing a time out for the user and also making the UI unusable while the upload was processing. So I thought I'd just call my import method like this:

ThreadPool.QueueUserWorkItem(this.NameOfMyImportMethod);

The data that the import method needs to work on has been as set as class variables when the Import class is constructed (I'm not doing the import work in the code behind!).

This all works fine most of the time. However seemingly at random the import method does NOT get called asynchronously, the browser sits waiting for a response and eventually times out.

I'm ensuring I'm catching all exceptions inside the import method.

I can't recreate it all time but it seems to happen mostly if I play about with the form causing a few post backs before actually submitting.

Any ideas as to what might be going on here?

Thanks for any help!

+1  A: 

This is a bit of a longshot (particularly if you're seeing the problem in your development environment where there shouldn't be a lot of competition for these threads), but you may be running out of thread pool threads and/or getting deadlocked waiting for them to become available.

You can check by inserting something like the following in your page, perhaps right after queuing the delegate:

int workerThreads;
int maxWorkerThreads;
int completionPortThreads;
int maxCompletionPortThreads;

ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);

System.Diagnostics.Debug.WriteLine(string.Format("There are {0} of {1} worker threads available.\r\n", workerThreads, maxWorkerThreads));

Are you using the ThreadPool elsewhere in the page?

Jeff Sternal
+1  A: 

I suggest to use Async Pages or Async Hanlder for uploading files.

About uploading files look here: http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx

dario-g
A: 

Thanks for the responses - though I'm going to (kind of) answer this myself. I ended up just creating a new thread manually - it seems to have fixed the problem. It's not a great solution and I've still no idea why this was happening.

bplus