一个实时的最新进展添加到asp.net一个缓慢的页面 [英] Add a realtime progress update to a slow page in asp.net

查看:108
本文介绍了一个实时的最新进展添加到asp.net一个缓慢的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个实时的进度报告一个缓慢的加载页面添加到我的C#/ asp.net 4.0应用程序。我已经看的UpdatePanel和的UpdateProgress Ajax控件,但我不认为他们是合适的。

I'm trying to add a realtime progress report to my c#/asp.net 4.0 application for a slow loading page. I've look at the UpdatePanel and UpdateProgress Ajax controls but I don't think they're suitable.

基本上,当用户点击一个按钮的页面执行多项任务,我希望用户看到更新为每一位完成,而不是报告,他们都完成,页面加载完成时。

Basically when the user click a button the page executes a number of tasks, I'd like the user to see an update as each one completes, instead of a report when they all complete and the page load completes.

订单事情会发生将是:
1.用户点击按钮启动
2.调用方法1
3.如果方法1结束,用户看到方法1完成
3.调用方法2
等。

The order thing would happen would be: 1. user click button to start 2. call method 1 3. when method 1 completes, user see "Method 1 done" 3. call method 2 etc.

任何人都可以在这方面帮助?

Can anyone help with this?

推荐答案

这有点异步执行的可能很难实现。把我的头顶部的几个解决方案:

This sort of asynchronous execution can be difficult to implement. A few solutions off the top of my head:

完全异步没有Ajax:


  1. 使用点击按钮,提交页面。

  2. Server生成该任务的GUID,并创建你的数据库中的记录。这可能包括:

  1. Use hits button, submits page.
  2. Server generates a GUID for the task, and creates a record in your database. This might include:


  • 的Guid(ID)

  • 状态标志/枚举

  • 开始时间。

服务器产生一个线程来处理任务,并传入GUID。

Server spawns a thread to handle the task and passes in the Guid.

在在步骤中创建的螺纹(3):

In the thread created in step (3):


  1. 线程启动

  2. 阅读从数据库中记录的当前状态

  3. 执行基于状态的下一步

  4. 更新数据库状态以表示它已经准备好做下一个步骤,或者设置错误标志。

  5. 休眠几毫秒,以保持阻塞应用程序(可能是不必要的 - 我不知道线程在IIS下如何互动)

  6. 循环(2),直到一切都完成了。

  7. 线程退出。

这里是很容易与一个lambda

(new Thread(
    () => {
        DoLongRunningWork();
    }
) { Name = "Long Running Work Thread"
    ,
    Priority = ThreadPriority.BelowNormal 
    }).Start();

同步

更容易,但可能会导致一些性能问题:

Easier, but might cause some performance problems:


  1. 用户提交了开始的形式。

  2. 服务器写入启动...来响应流并刷新流。这应该得到的文本返回给客户端,我想,但我已经很多年没有尝试过。

  3. 服务器执行的第一步。

  4. 服务器写入的状态响应流和刷新。

  5. 循环步骤(3),直到完成。

实际上,页面保持连接打开,直到任务完成后,和冲洗输出定期保持超时客户端。这可能与超时等,和你的服务器配置(输出缓冲等)的问题可能是一个问题。

Effectively, the page keeps the connection open until the task is complete, and flushing the output periodically keeps the client from timing out. This may have problems with timeouts, etc, and your server configuration (output buffering, etc) might be an issue.

后台任务

类似于第一异步方法:


  1. 用户点击开始

  2. 服务器会将一行添加到标识要执行的任务,检索该ID的数据块,并将其返回给客户机。

  3. 创建计划任务(脚本,窗口服务等)的调查表,执行所需的任务,因为它的进展更新状态。

  4. 客户端重新与职位周期性DB ID的形式。服务器会针对数据库的ID,并返回一个消息有关的状态(包括约previous步骤,如EXEC时间,ETA等信息)

  5. 客户端环路(4),直到任务完成或错误的。

这和第一种方法的区别在于,该螺纹生活在一个独立的进程,而不是IIS。

The difference between this and the first approach is that the thread lives in a separate process instead of IIS.

每个方法都有其问题,当然,有可能做到这一点的简单方法。

Each approach has its issues, of course, and there may be a simpler way to do this.

这篇关于一个实时的最新进展添加到asp.net一个缓慢的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆