ThreadPool.QueueUserWorkItem - 奇怪的行为(Asp.Net)
我目前正在 Asp.Net 应用程序中使用 ThreadPool.QueueUserWorkItem。 基本上,用户使用带有 FileUpload 控件的表单上传文件。 该文件可能需要相当长的时间来处理,这会导致用户超时,并且在上传处理时使 UI 无法使用。 所以我想我应该像这样调用我的导入方法:
ThreadPool.QueueUserWorkItem(this.NameOfMyImportMethod);
导入方法需要处理的数据在构造导入类时已设置为类变量(我没有在后面的代码中进行导入工作) !)。
这在大多数情况下都运行良好。然而,导入方法似乎是随机的,不会被异步调用,浏览器会等待响应并最终超时。
我确保捕获导入方法中的所有异常。
我无法一直重新创建它,但如果我在实际提交之前使用表单导致一些回发,那么似乎大多数情况都会发生。
关于这里可能发生的事情有什么想法吗?
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这有点不太可能(特别是如果您在开发环境中看到问题,这些线程不应该有太多竞争),但您可能会耗尽线程池线程和/或陷入死锁等待它们可用。
您可以通过在页面中插入类似以下内容来进行检查,也许是在对委托进行排队之后立即插入:
您是否在页面的其他位置使用
ThreadPool
?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:
Are you using the
ThreadPool
elsewhere in the page?我建议使用 异步页面 或 异步处理程序 用于上传文件。
关于上传文件请看这里: http://weblogs.asp.net/jgalloway/archive/2008/01/08/large-file-uploads-in-asp-net.aspx
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.