在 ASP.NET 中处理数据时如何更新进度消息?

发布于 2024-08-27 00:32:32 字数 115 浏览 3 评论 0原文

我有一个流程,它输出逐步消息(即,处理项目 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 技术交流群。

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

发布评论

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

评论(2

微暖i 2024-09-03 00:32:32

这不是线程问题,而是 Web 浏览器 UI 问题。您希望浏览器在您在服务器上工作时呈现状态。理论上,您可以执行以下操作:

Response.Write("something");
Response.Flush();

但是 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 中无效:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Buffer = false;
    Response.Clear();
    Response.Write("<html><body>");
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Response.Write("</body></html>");
    Response.End();
}

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:

Response.Write("something");
Response.Flush();

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:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Buffer = false;
    Response.Clear();
    Response.Write("<html><body>");
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Thread.Sleep(2000);
    Response.Write("1\n");
    Response.Flush();
    Response.Write("</body></html>");
    Response.End();
}
铁轨上的流浪者 2024-09-03 00:32:32

使用两种方式的通信方法,例如 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.

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