使用 Javascript 进行长流程的进度更新

发布于 2024-08-24 17:55:28 字数 189 浏览 4 评论 0原文

我有一个 ASP.NET 页面,它调用一个 DLL,该 DLL 将启动一个更新产品信息的长进程。当流程运行时,我希望为用户提供有关该流程所在的产品以及产品状态的持续更新。我一直很难让它发挥作用。我将信息添加到日志文本文件中,我想我应该重定向到一个新页面,并让该页面使用 Javascript 每隔几秒从文本文件中读取一次。我的问题是有其他人尝试过这个并且效果好吗?

I have an asp.net page that calls a dll that will start a long process that updates product information. As the process is running, I want to provide the user with constant updates on which product that process is on and the status of the products. I've been having trouble getting this to work. I add information to a log text file and I was thinking that I'd redirect to a new page and have that page use Javascript to read from the text file every few seconds. My question is has anyone else tried this and does it work fairly well?

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

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

发布评论

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

评论(3

梦回梦里 2024-08-31 17:55:28

我将使用 Ajax 并轮询该文本文件以获取更新。

I would use Ajax and poll that text file for updates.

巴黎盛开的樱花 2024-08-31 17:55:28
  • 进行长流程,以便更新有关进度的某些状态 - 状态、最后处理的产品等。
  • 一旦开始,您可能只想在每处理 50 个项目时更新进度,以节省资源(您可能不会,这取决于在 ASP.NET 端。
  • 如果处理与当前会话关联,您可能希望将状态放入会话中。如果它不是 - 例如全局 - 将其置于某种全局状态。
  • 然后通过 JavaScript 中的 Ajax 偶尔轮询一次状态,例如使用 JSON 并相应地更新 UI。
  • 确保在访问共享状态时使用正确的锁定。
  • 另外,尝试保持状态较小(仅存储获取 js gui 数据绝对需要的内容)。
  • Make the long process so that it updates some state about the progress - status, last product processed, etc.
  • Once started, you might want to update the progress on only say every 50th item processed, to spare resources (you might not, it depends on what you want) on the ASP.NET side.
  • If the processing is associated with the current session, you might want to put the state in the session. If it is not - e.g. global - put it in some global state.
  • Then poll the state once in a while through Ajax from javascript, using e.g. JSON and update the UI accordingly.
  • Make sure you use proper locking when accessing the shared state.
  • Also, try to keep the state small (store only what is absolutely required to get the data for the js gui).
黯然 2024-08-31 17:55:28

您可以将状态输出到数据库表,而不是将状态输出到基于文本的日志文件(或者,除了使用日志文件之外;您可以同时使用两者)。例如,按如下方式构造表:

Queue:
  id (Primary Key)
  user_id (Foreign Key)
  product_id (Foreign Key) //if available
  batch_size //total set of products in the batch this message was generated from
  batch_remaining //number remaining in this batch
  message

现在您有一个队列。现在,当用户进入特定页面时,您可以执行以下两种操作之一:

  1. 从队列中获取一些相关数据并将其显示在标题中。每次刷新页面时都会更新。
  2. 创建一个 AJAX 处理程序,它将轮询 Web 服务以定期获取数据。这样,即使用户在同一页面上而无需刷新,您也可以每 15 或 20 秒更新一次状态。

您可以在发送行后将其从队列中删除,或者如果您想保留该数据,请添加另一个名为 sent 的列,并在发送后将其设置为 true用户已经看到该数据。

Rather than outputting the status to a text based log file (or, in addition to using the log file; you can use both), you could output the status to a Database table. For example, structure the table as so:

Queue:
  id (Primary Key)
  user_id (Foreign Key)
  product_id (Foreign Key) //if available
  batch_size //total set of products in the batch this message was generated from
  batch_remaining //number remaining in this batch
  message

So now you have a queue. Now when a user goes on a particular page, you can do one of two things:

  1. Grab some relevant data from the Queue and display it in the header. This will be updated every time the page is refreshed.
  2. Create an AJAX handler that will poll a web service to grab the data at intervals. This way you can have the status update every 15 or 20 seconds, even if the user is on the same page without refreshing.

You can delete the rows from the Queue after they've been sent, or if you want to retain that data, add another column called sent and set it to true once the user has seen that data.

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