服务器_asp.net 在客户端显示服务器端任务处理进度条的探讨,下面就是采用静态变量的方法
}
while (Processbar < TotalCount)
}
{
{
}
{
<body>
}
<div></div>
document.getElementById("processbar2").style.width = "0%";
下面这这种方法,是常用的方法,一般情况下可以满足需求:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -
}
}
{
<div></div>
Processbar = 0;
Response.End();
function startTask() {
/// <summary>
count = 1;
private void SaveData()
http.send(null);
/// <summary>
}
{
Response.ClearContent();
WriteLog();
}
{
<head runat="server">
if (Request.QueryString["step"] != null && Request.QueryString["step"].Equals(String.Empty) == false)
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessTask));
else
{
{
http.onreadystatechange = function() {
document.getElementById("process").innerHTML = "0%";
http = createXMLHTTP();
document.getElementById("processbar2").style.width = http.responseText + "%";
private String key;
</script>
}
G.Text = key;
}
document.getElementById("process").innerHTML = document.getElementById("processbar1").innerHTML = "完成";
if ("ok" == http.responseText) {
Response.End();
{
{
<div>
</body>
{
}
<html xmlns="http://www.w3.org/1999/xhtml ">
}
Response.Write("ok");
// 所有的任务都完成了,输出结束信息,终止前端的请求。
}
}
<%@ Page Language="C#" %>
var timer = null;
Response.ClearContent();
function showProcess() {
<head runat="server">
/// 得到执行过程的阶段
<%@ Page Language="C#" %>
window.clearInterval(timer);
var timer = null;
那么终极的方法是什么呢?对,将程序执行进度保存在第三方的存储介质上,如数据库,文件系统等等都是可以的。这个方法代码我就不写了,就是增加访问数据库的部分即可。
<script runat="server">
/// <summary>
thread.Start();
if (Request.QueryString["step"] != null && Request.QueryString["step"].Equals(String.Empty) == false)
TotalCount = 150;
<input type="button" value="启动处理长时间操作" />
<html xmlns="http://www.w3.org/1999/xhtml ">
<script type="text/javascript">
private static int Processbar = 0;
http.responseText + "%";
<title>在客户端显示服务器端任务处理进度条的探讨</title>
下面就是采用静态变量的方法实现的:
if (Processbar < TotalCount)
}
if (String.IsNullOrEmpty(data))
</html>
}
document.getElementById("processbar2").style.width = "100%";
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessTask));
}
}
else
<input type="button" value="开始处理长时间操作" />
// 开始执行任务的请求,启动长时间的任务处理。
var count = 1;
Response.Write(0);
}
if (http.readyState == 4 && http.status == 200)
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(key + ".txt"), true);
document.getElementById("process").innerHTML = document.getElementById("processbar1").innerHTML =
String data = Convert.ToString(Cache.Get(key));
else
return window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject("MSXML2.XMLHTTP");
Response.Write("ok");
/// 将过程保存。
document.getElementById("process").innerHTML = document.getElementById("processbar1").innerHTML = "0%";
/// <summary>
</script>
}
/// </summary>
var guid = "<asp:Literal runat='server'/>";
{
http.open("GET", "<%=Request.Url.ToString() %>?step=" + (count++) + "&" + Date.parse(new Date()), true);
protected void ProcessTask()
{
if (Request.QueryString["step"].Equals("1"))
private void WriteLog()
复制代码 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -transitional.dtd">count = 1;
function startTask() {
while (Processbar < TotalCount)
//通过计算,得出TotalCount的值,比如查询数据库等
<div></div>
protected void Page_Load(object sender, EventArgs e)
timer = window.setInterval("showProcess()", 1000);
}
<div></div>
());
/// </summary>
// 输出处理的过程
if ("ok" == http.responseText) {
}
<script type="text/javascript">
代码执行效果:
<form runat="server">
</html>
function showProcess() {
</script>
}
if (String.IsNullOrEmpty(key)) key = Guid.NewGuid().ToString();
Processbar = this.GetProcessbar() + 5; //这里只是模拟一下,每次加 5
{
private static int TotalCount = 100; //设置初始值,防止出现被0除。
}
{
</script>
}
Response.ClearContent();
<body>
TotalCount = 200; //假如完成一个任务需要200个步骤
{
if (String.IsNullOrEmpty(data))
return 0;
/// <returns></returns>
}
Processbar = 0;
private int Processbar = 0; //设置初始的状态,也可以代表一系列步骤中的每个步骤。
System.Threading.Thread.Sleep(1000);
return Convert.ToInt32(data.Split(',')[1]);
//通过计算,得出TotalCount的值,比如查询数据库等,也可以是一个任务的多个步骤的总和。
sw.WriteLine("Processbar = " + Processbar + " TotalCount = " + TotalCount + " " + System.DateTime.Now.ToString
Response.End();
var http = null;
function createXMLHTTP() {
System.IO.File.Delete(Server.MapPath(key + ".txt"));
/// 设置全局变量,以便不同的方法是用
Response.Write(Processbar * 100 / TotalCount);
Response.ClearContent();
<title>在客户端显示服务器端任务处理进度条的探讨</title>
但是,这种方法就是万事大吉了吗?完全错误,这种方法仍然存在显示不准确的现象,造成显示不准确的原因就是 Cache 的使用,IIS 6之后,增加了应用程序池的功能,这个功能可以大大提高程序的性能,减少程序本身的错误导致的整个网站的崩溃。但是,如果应用程序池的“性能”-“Web 园”数目设置大于1的情况下,HttpApplicationState(Application)、Cache、HttpSessionState(Session)这些变量都是都是无法使用了,这是因为:每个Web 园会启动一个w3wp.exe工作进程,每个工作进程之间是相互独立的,以上这些变量也就是不是共享的了,所以,使用Cache保存程序执行进度的方法也是不完全正确的。
/// </summary>
else
//以下判断原来判断请求的不同过程,是第一次请求,还是更新进度条的请求,实现方法也可以划分为多个程序来实现。
return false;
sw.Close();
else {
String data = Convert.ToString(Cache.Get(key));
{
protected void ProcessTask()
function createXMLHTTP() {
else
}
}
document.getElementById("process").innerHTML = http.responseText + "%";
private int TotalCount = 100; //设置初始值,防止出现被0除。
</head>
{
Cache.Insert(key, Processbar.ToString() + "," + TotalCount.ToString());
{
if (Request.QueryString["step"].Equals("1"))
{
</body>
var count = 1;
Processbar = this.GetProcessbar();
}
</head>
{
key = Request.QueryString["guid"]; //多个并发请求时,用来区分客户端的请求。
return false;
http.setRequestHeader("Connection", "close");
private int GetProcessbar()
Response.Write(0);
return Convert.ToInt32(data.Split(',')[0]);
Processbar += 5;
http.open("GET", "<%=Request.Url.ToString() %>?step=" + (count++) + "&guid=" + guid + "&" + Date.parse(new
}
private int GetTotalCount()
System.Threading.Thread.Sleep(1000); //这里只是模拟一个长时间的执行过程。
var http = null;
}
Date()), true);
return 0;
<script runat="server">
}
transitional.dtd">
{
}
else {
TotalCount = this.GetTotalCount();
}
/// <returns></returns>
<form runat="server">
/// </summary>
}
}
/// 得到全部的过程数
http.send(null);
else
SaveData();
protected void Page_Load(object sender, EventArgs e)
{
http = createXMLHTTP()
if (System.IO.File.Exists(Server.MapPath(key + ".txt")))
return window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject("MSXML2.XMLHTTP");
</div>
if (http.readyState == 4 && http.status == 200)
thread.Start();
if (Processbar < TotalCount)
Response.Write(Processbar * 100 / TotalCount);
{
</form>
Cache.Remove(key);
document.getElementById("process").innerHTML = "完成";
SaveData();
Response.End();
window.clearInterval(timer);
http.onreadystatechange = function() {
else
timer = window.setInterval("showProcess()", 1000);
复制代码 代码如下:
</form>本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/server/yun/6087.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
租用云服务器后的备案问
时间:2021-01-05
-
百度云服务器bcc有什么优
时间:2021-01-05
-
什么是云服务器cvm?怎么
时间:2021-01-05
-
云服务器怎么保证信息安
时间:2021-01-05
-
云服务器怎么预防被攻击
时间:2021-01-05
-
阿里云ECS实例设置用户r
时间:2020-12-29
-
阿里云ECS服务器入门使用
时间:2020-12-29
-
怎么配置云服务器
时间:2020-12-28
热门文章
-
租用云服务器后的备案问题你真的了解吗
时间:2021-01-05
-
选择美国云服务器需要关注什么?
时间:2020-12-27
-
阿里云服务器怎么买?阿里云服务器购买
时间:2020-12-25
-
运维必须知道的关于云服务器的十个问题
时间:2020-12-24
-
如何快速搭建一个阿里云服务器
时间:2020-12-24
-
什么是云?什么是云服务?什么是云主机
时间:2020-12-25
-
云服务器网站承载量一般有多大?一个云
时间:2020-12-28
-
浅谈云服务器和独立服务器的八大差异
时间:2020-12-24
-
SugarHosts云服务器如何开启访问端口和使用
时间:2020-12-24
-
云服务器是什么,云主机干什么用的?
时间:2020-12-28
