ThreadPool.QueueUserWorkItem - 奇怪的行为(Asp.Net)

发布于 2024-08-20 23:57:34 字数 479 浏览 4 评论 0原文

我目前正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美胚控场 2024-08-27 23:57:34

这有点不太可能(特别是如果您在开发环境中看到问题,这些线程不应该有太多竞争),但您可能会耗尽线程池线程和/或陷入死锁等待它们可用。

您可以通过在页面中插入类似以下内容来进行检查,也许是在对委托进行排队之后立即插入:

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));

您是否在页面的其他位置使用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:

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?

离线来电— 2024-08-27 23:57:34

感谢您的回复 - 尽管我将(有点)自己回答这个问题。
我最终只是手动创建了一个新线程 - 它似乎已经解决了问题。
这不是一个很好的解决方案,我仍然不知道为什么会发生这种情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文