在 ASP.NET 中处理数据时如何更新进度消息?
我有一个流程,它输出逐步消息(即,处理项目 1...项目 2 中的错误等)。
我希望在整个过程中(而不是最后)将其输出给用户。
我很确定我需要使用线程来完成此操作,但找不到合适的示例。
I have a process, that outputs step by step messages (i.e., Processing item 1... Error in item 2 etc etc).
I want this to be output to the user during the process, and not at the end.
I'm pretty sure I need to do this with threading, but can't find a decent example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是线程问题,而是 Web 浏览器 UI 问题。您希望浏览器在您在服务器上工作时呈现状态。理论上,您可以执行以下操作:
但是
Flush()
不会确保浏览器当时实际呈现您的代码。实际上,您无法控制数据从服务器到浏览器的缓存/分块/缓冲方式。因此每次更新都应该是一个“完整的”http 事务。一种方法(也是常见的方法)是使用 AJAX 来实现此目的。用户单击一个按钮来启动一些后台工作,并且您有一个 JavaScript 计时器,它会轮询(发出请求)以检查工作的状态,并更新客户端浏览器。
查看 Real-Time Progress Bar With ASP.NET AJAX 来制作 ajax 进度指示器使用ajax和.net。
本文中有一个使用 http 处理程序创建进度条的绝佳示例: http ://www.asp101.com/articles/matt/progressbar/default.asp
为了证明我的观点,以下代码在 Firefox 中有效,但在 IE 或 Chrome 中无效:
It's not a threading issue, but a web browser UI issue. You want the browser to render the status as you are doing work on the server. In theory you could do something like:
but the
Flush()
won't ensure the browser actually renders your code at that moment. In reality you cannot control how data is cached/chunked/buffered underway from the server to the browser. So each update should be a 'full' http transaction.One way, and common one, is to use AJAX to achieve this. The user clicks a button which starts some background work, and you have a javascript timer which polls (makes requests) to check the status of the work, and updates the client browser.
Check out Real-Time Progress Bar With ASP.NET AJAX for doing an ajax progress indicator with ajax and .net.
There's an excellent example of creating a progress bar with a http handler in this article: http://www.asp101.com/articles/matt/progressbar/default.asp
To prove my point, the following code works in Firefox, but not in IE or Chrome:
使用两种方式的通信方法,例如 webRTC 或 signalR 或 gRPC,将实时消息从服务器发送到客户端 Web 浏览器。
use two way communication methods, such as webRTC or signalR or gRPC, send live messages from server to client web browser.