开发者

ThreadPool.QueueUserWorkItem - strange behaviour (Asp.Net)

开发者 https://www.devze.com 2022-12-19 08:55 出处:网络
I\'m currently using ThreadPool.QueueUserWorkItem in an Asp.Net application. Basically a user uploads a file using a form with an FileUpload Control.

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开发者_StackOverflow社区 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!


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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号