如何更新进度的消息,而在ASP.NET处理数据? [英] How can I update progress messages while processing data in 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.
我是pretty知道我需要线程要做到这一点,但无法找到一个体面的例子。
I'm pretty sure I need to do this with threading, but can't find a decent example.
推荐答案
这不是一个线程问题,而是一个网络浏览器的用户界面问题。您希望浏览器中呈现的状态,你在服务器上做的工作。从理论上讲,你可以这样做:
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();
但同花顺()
将无法确保浏览器实际上是在那一刻呈现您code。在现实中你无法控制数据如何缓存/分块/从服务器到浏览器的缓存正在进行中。所以,每次更新应该是一个'完整'的HTTP事务。
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.
一种方法和普通的一种,是使用AJAX来实现这一目标。用户点击一个按钮,启动一些后台的工作,你有一个JavaScript定时器,轮询(发出请求),以检查工作的状态,并更新客户端浏览器。
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.
查看实时进度条随着ASP.NET AJAX 做一个ajax进度指示器使用Ajax和.NET。
Check out Real-Time Progress Bar With ASP.NET AJAX for doing an ajax progress indicator with ajax and .net.
有是创建在这篇文章中一个HTTP处理程序的进度条的一个很好的例子:的 http://www.asp101.com/articles/matt/progressbar/default.asp
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
要证明我的观点,在Firefox下code ++工程,但不是在IE或Chrome:
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();
}
这篇关于如何更新进度的消息,而在ASP.NET处理数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!