长时间运行的服务器进程更新客户端的标签。AJAX ASP.Net

时间:2021-10-26 15:53:38

I have a long running server side process (gets data via a web service in batches as part of a do/while loop). On each pass through the loop I want to push a status message back to a label control in an update panel on the client.

我有一个长时间运行的服务器端进程(作为do/while循环的一部分,通过web服务批量获取数据)。在每个循环中,我希望将状态消息推回到客户端更新面板中的标签控件。

I've tried assigning the value to the label in code behind and then using a timer control to refresh the update panel every minute. But that doesn't seem to work.

我尝试将值分配给后面的代码中的标签,然后使用计时器控件每分钟刷新一次更新面板。但这似乎行不通。

Any ideas or suggestions?

有什么想法和建议吗?

TIA

蒂雅

4 个解决方案

#1


1  

I suggest using a simple jQuery.get call to a page that reads the status from your server side process probably by way of a session variable. Then the client would make the request. The failure is that you are having your server side try to push content to the client (forcing the update) from the server side...as the long running process continues to run. To my knowledge you can't push via http at your whim...the request must be made by the browser!

我建议使用简单的jQuery。获取对页面的调用,该页面可能通过会话变量从服务器端进程读取状态。然后客户端发出请求。失败之处在于,您让服务器端尝试从服务器端将内容推送到客户端(强制更新)……随着长时间运行的进程继续运行。据我所知,你不能随心所欲地通过http进行推送……请求必须由浏览器发出!

#2


1  

You're on the right track in your comment. Consider spawning the work in a separate thread and tracking its progress through the Session/Cache. You could even go a step further and encapsulate the task and all of its threading logic into a LongRunningTask class, and in your polling code just check ((LongRunningTask)Cache["MyLongRunningTask"]).TaskStatus or something along those lines (of course - don't use that exact code to retrieve the status =))

你的评论是正确的。考虑在单独的线程中生成工作,并通过会话/缓存跟踪其进度。您甚至可以更进一步,将任务及其所有线程逻辑封装到一个LongRunningTask类中,在轮询代码中检查(LongRunningTask)缓存[“MyLongRunningTask”]。TaskStatus或类似的东西(当然,不要使用精确的代码来检索status =))

#3


0  

You might consider a slightly different approach.

您可以考虑一种稍微不同的方法。

First, get rid of the timer. Then use Response.Write to output where you are in the process. I.E.: Processing Item Number 234

首先,去掉计时器。然后使用响应。在处理过程中从写入到输出。即:处理项目编号234

After the response.write, do a Response.Flush. This forces the server to write the currently cached response object to the browser.

后的反应。写,Response.Flush。这迫使服务器将当前缓存的响应对象写入浏览器。

#4


0  

Have your server process Write and Flush a

让您的服务器进程写并刷新a

<script>
  /* jquery to update an element on the client page */
</script>

to update the element on the page with the current status. You may have to pad this with spaces so it adds up to enough bytes to get flushed correctly (like, say 1000bytes) . This can come out even after the . Your client may have a bit of an issue with this, if the stuff inside the script tags is very verbose, and you have a LOT of updates, but if you keep it just to a handful of updates it should be fine (like.... every 10th of a percent through), otherwise the filesize of the final output may be in the high megabytes range.

使用当前状态更新页面上的元素。您可能需要用空格来填充它,这样它就可以添加足够的字节来正确地刷新(例如,1000bytes)。这个可以在。您的客户端可能有点问题,如果脚本标签内的内容非常详细,你有很多的更新,但如果你一直只是少量的更新应该没事的(如....每百分之十通过),否则最终输出的filesize可能在高兆字节范围内。

#1


1  

I suggest using a simple jQuery.get call to a page that reads the status from your server side process probably by way of a session variable. Then the client would make the request. The failure is that you are having your server side try to push content to the client (forcing the update) from the server side...as the long running process continues to run. To my knowledge you can't push via http at your whim...the request must be made by the browser!

我建议使用简单的jQuery。获取对页面的调用,该页面可能通过会话变量从服务器端进程读取状态。然后客户端发出请求。失败之处在于,您让服务器端尝试从服务器端将内容推送到客户端(强制更新)……随着长时间运行的进程继续运行。据我所知,你不能随心所欲地通过http进行推送……请求必须由浏览器发出!

#2


1  

You're on the right track in your comment. Consider spawning the work in a separate thread and tracking its progress through the Session/Cache. You could even go a step further and encapsulate the task and all of its threading logic into a LongRunningTask class, and in your polling code just check ((LongRunningTask)Cache["MyLongRunningTask"]).TaskStatus or something along those lines (of course - don't use that exact code to retrieve the status =))

你的评论是正确的。考虑在单独的线程中生成工作,并通过会话/缓存跟踪其进度。您甚至可以更进一步,将任务及其所有线程逻辑封装到一个LongRunningTask类中,在轮询代码中检查(LongRunningTask)缓存[“MyLongRunningTask”]。TaskStatus或类似的东西(当然,不要使用精确的代码来检索status =))

#3


0  

You might consider a slightly different approach.

您可以考虑一种稍微不同的方法。

First, get rid of the timer. Then use Response.Write to output where you are in the process. I.E.: Processing Item Number 234

首先,去掉计时器。然后使用响应。在处理过程中从写入到输出。即:处理项目编号234

After the response.write, do a Response.Flush. This forces the server to write the currently cached response object to the browser.

后的反应。写,Response.Flush。这迫使服务器将当前缓存的响应对象写入浏览器。

#4


0  

Have your server process Write and Flush a

让您的服务器进程写并刷新a

<script>
  /* jquery to update an element on the client page */
</script>

to update the element on the page with the current status. You may have to pad this with spaces so it adds up to enough bytes to get flushed correctly (like, say 1000bytes) . This can come out even after the . Your client may have a bit of an issue with this, if the stuff inside the script tags is very verbose, and you have a LOT of updates, but if you keep it just to a handful of updates it should be fine (like.... every 10th of a percent through), otherwise the filesize of the final output may be in the high megabytes range.

使用当前状态更新页面上的元素。您可能需要用空格来填充它,这样它就可以添加足够的字节来正确地刷新(例如,1000bytes)。这个可以在。您的客户端可能有点问题,如果脚本标签内的内容非常详细,你有很多的更新,但如果你一直只是少量的更新应该没事的(如....每百分之十通过),否则最终输出的filesize可能在高兆字节范围内。