asp.net 中的线程 (C#)

发布于 2024-10-24 07:54:00 字数 851 浏览 1 评论 0原文

当用户访问 .aspx 页面时,我需要在新线程中启动一些后台计算。计算结果需要存储在用户的会话中,以便在回调时可以检索结果。此外,在回调中,我需要能够查看后台计算的状态。 (例如,我需要检查计算是否已完成并成功完成,或者是否仍在运行)我该如何完成此操作?

问题

  1. 我如何检查线程的状态?多个用户可以同时运行后台计算,因此我不确定知道哪个线程属于哪个用户的过程将如何工作。(尽管在我的场景中,唯一重要的线程是最初由用户 A——用户 A 执行回调来检索/检查该线程的状态)。
  2. 我的假设是否正确,将用户的 HttpSessionState“会话”变量传递给新线程将按我的预期工作(例如,我可以稍后向他们的会话添加内容)。

谢谢。另外我不得不说,我可能对某些事情感到困惑,但现在 SO 登录系统似乎有所不同,所以我无法访问我的旧帐户。

编辑

我现在正在考虑使用这篇文章基本上使用一个类和一个单例来管理线程列表。我可能不会将数据存储在数据库中(并导致与检索数据以及数据库中的额外表、维护等相关的性能损失),我可能也会将数据存储在我的类中。

编辑 2

我在第一次编辑中提到的方法效果很好。此外,我还有计时器来确保线程及其关联数据在相应的计时器调用其清理方法后都被清理。包含我的数据和线程的对象存储在 Singleton 类中。对于某些应用程序来说,使用数据库进行存储可能是合适的,但对我来说似乎有点过分了,因为我的数据与页面的特定实例相关联,并且在该页面上下文之外毫无用处。

When a user visits an .aspx page, I need to start some background calculations in a new thread. The results of the calculations need to be stored in the user's Session, so that on a callback, the results can be retrieved. Additionally, on the callback, I need to be able to see what the status of the background calculation is. (E.g. I need to check if the calculation is finished and completed successfully, or if it is still running) How can I accomplish this?

Questions

  1. How would I check on the status of the thread? Multiple users could have background calculations running at the same time, so I'm unsure how the process of knowing which thread belongs to which user would work.. (though in my scenario, the only thread that matters, is the thread originally started by user A -- and user A does a callback to retrieve/check on the status of that thread).
  2. Am I correct in my assumption that passing an HttpSessionState "Session" variable for the user to the new thread, will work as I expect (e.g. I can then add stuff to their Session later).

Thanks. Also I have to say, I might be confused about something but it seems like the SO login system is different now, so I don't have access to my old account.

Edit

I'm now thinking about using the approach described in this article which basically uses a class and a Singleton to manage a list of threads. Instead of storing my data in the database (and incurring the performance penalty associated with retrieving the data, as well as the extra table, maintenance, etc in the database), I'll probably store the data in my class as well.

Edit 2

The approach mentioned in my first edit worked well. Additionally I had timers to ensure the threads, and their associated data, were both cleaned up after the corresponding timers called their cleanup methods. The Objects containing my data and the threads were stored in the Singleton class. For some applications it might be appropriate to use the database for storage but it seemed like overkill for mine, since my data is tied to a specific instance of a page, and is useless outside of that page context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

甜妞爱困 2024-10-31 07:54:00

期望会话状态在这种情况下继续工作;工作人员可能不知道用户是谁,即使知道(或更可能:您将这些数据捕获到工作人员中),也没有理由存储任何内容(更新会话是迈向请求管道结束的一步;但是如果您不在管道中...?)。

我怀疑您可能需要使用用户的某些唯一属性(他们的 id 或 cn)单独存储这些数据,或者发明一个 GUID。在单台机器上,将其存储在同步字典(或类似的字典)中可能就足够了,但在场/集群上,您可能需要将数据向下推送到数据库或状态服务器。并手动获取。

I would not expect session-state to continue working in this scenario; the worker may have no idea who the user is, and even if it does (or more likely: you capture this data into the worker), no reason to store anything (updating session is a step towards the end of the request pipeline; but if you aren't in the pipeline...?).

I suspect you might need to store this data separately using some unique property of the user (their id or cn), or invent a GUID otherwise. On a single machine it may suffice to store this in a synchronised dictionary (or similar), but on a farm/cluster you may need to push the data down a layer to your database or state server. And fetch manually.

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