ASP.Net 应用程序的轮询策略?

发布于 2024-07-16 02:45:47 字数 226 浏览 10 评论 0原文

我有一个 ASP.Net 应用程序,需要由另一台机器执行一些工作。 为此,我在两台机器都可见的队列上留下一条消息。 工作完成后,一条消息将留在第二个队列中。

我需要 ASP.Net 应用程序定期检查第二个队列以查看是否有任何任务已完成。

除了这样的循环,哪里是最好的地方呢? 全球.asax?

我记得在某处读过,你可以在一段时间间隔后调用一个函数。 那样合适吗?

I have an ASP.Net application that needs needs to have some work performed by another machine. To do this I am leaving a message on queue visible to both machines. When the work is done a message is left on second queue.

I need the ASP.Net application to check the second queue periodically to see if any of the tasks are complete.

Where is the best place to but such a loop? Global.asax?

I remember reading somewhere that you can get a function called after an interval. Would that be suitable?

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

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

发布评论

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

评论(2

因为看清所以看轻 2024-07-23 02:45:47

为了在 ASP.NET 上实现定期任务,我发现了两种可接受的方法:

  1. 在 Global.asax 的 Application_Start 期间,在 while 循环中生成一个线程 (1) 执行工作 (2) 将线程休眠一段时间。
  2. 再次在 Application_Start 中,将一个虚拟项插入到 ASP.NET 缓存中,在一定的时间间隔内过期,并为该缓存项提供一个在过期时调用的回调。 在该回调中,您可以完成工作并以相同的方式将缓存项插入回来。

在这两种方式中,您都需要确保即使发生错误,您的线程也能继续工作。 您可以在 SessionStart 和 BeginRequest 中放置恢复代码来检查您的线程或缓存项是否存在,并在发生问题时更新它。

To achieve periodical tasks on asp.net, I've found two acceptable approaches:

  1. Spawn a thread during Application_Start at global.asax, in a while loop (1) Do the work (2) Sleep the thread for an interval.
  2. Again in Application_Start, insert a dummy item into asp.net cache, expires in a certain interval and give that cache item a callback to be called when it's expired. In that callback, you can do the work and insert the cache item back the same way.

In both ways, you need to make sure that your thread keeps working even if there happens an error. You may place a restore code in SessionStart and BeginRequest to check your thread or cache item is there, and renew it if something has happened to it.

夜光 2024-07-23 02:45:47

我假设这是定期完成的,并且其他进程将项目放入队列中?

如果是这种情况,您可以在 Global.asax 中放入一些内容,在应用程序启动时创建一个单独的线程来简单地监视队列,您可以使用计时器让该线程休眠 X 秒,然后检查结果。

I assume that this is done on a regular basis, and that some other process puts the items on the queue?

If that is the case, you might put something in Global.asax that on application start creates a separate thread that simply monitors the queue, you could use a timer to have that thread sleep for X seconds, then check for results.

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