天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

asp.net 在客戶端顯示服務(wù)器端任務(wù)處理進(jìn)度條的探討

下面就是采用靜態(tài)變量的方法實(shí)現(xiàn)的:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -transitional.dtd">
<script runat="server">
private static int Processbar = 0;
private static int TotalCount = 100; //設(shè)置初始值,防止出現(xiàn)被0除。
protected void ProcessTask()
{
//通過計算,得出TotalCount的值,比如查詢數(shù)據(jù)庫等
TotalCount = 150;
while (Processbar < TotalCount)
{
Processbar += 5;
System.Threading.Thread.Sleep(1000);
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["step"] != null && Request.QueryString["step"].Equals(String.Empty) == false)
{
if (Request.QueryString["step"].Equals("1"))
{
Processbar = 0;
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessTask));
thread.Start();
Response.ClearContent();
Response.Write(0);
Response.End();
}
else
{
Response.ClearContent();
if (Processbar < TotalCount)
{
Response.Write(Processbar * 100 / TotalCount);
}
else
{
Response.Write("ok");
}
Response.End();
}
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml ">
<head runat="server">
<title>在客戶端顯示服務(wù)器端任務(wù)處理進(jìn)度條的探討</title>
<script type="text/Javascript">
var http = null;
var count = 1;
var timer = null;
function createXMLHTTP() {
return window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject("MSXML2.XMLHTTP");
}
function showProcess() {
http = createXMLHTTP()
http.open("GET", "<%=Request.Url.ToString() %>?step=" + (count++) + "&" + Date.parse(new Date()), true);
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200)
if ("ok" == http.responseText) {
document.getElementById("process").innerHTML = "完成";
window.clearInterval(timer);
}
else {
document.getElementById("process").innerHTML = http.responseText + "%";
}
}
http.send(null);
}

function startTask() {
count = 1;
document.getElementById("process").innerHTML = "0%";
timer = window.setInterval("showProcess()", 1000);
return false;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<input type="button" value="開始處理長時間操作" onclick="return startTask();" />
<div id="process"></div>
</form>
</body>
</html>

這種方法,在一個用戶訪問的情況下是沒有問題的,但多個用戶訪問時就會造成混亂。

下面這這種方法,是常用的方法,一般情況下可以滿足需求:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 -

transitional.dtd">

<script runat="server">
/// <summary>
/// 設(shè)置全局變量,以便不同的方法是用
/// </summary>
private int Processbar = 0; //設(shè)置初始的狀態(tài),也可以代表一系列步驟中的每個步驟。
private int TotalCount = 100; //設(shè)置初始值,防止出現(xiàn)被0除。
private String key;
protected void ProcessTask()
{
while (Processbar < TotalCount)
{
Processbar = this.GetProcessbar() + 5; //這里只是模擬一下,每次加 5
System.Threading.Thread.Sleep(1000); //這里只是模擬一個長時間的執(zhí)行過程。
SaveData();
}
}

protected void Page_Load(object sender, EventArgs e)
{
key = Request.QueryString["guid"]; //多個并發(fā)請求時,用來區(qū)分客戶端的請求。
if (String.IsNullOrEmpty(key)) key = Guid.NewGuid().ToString();
Processbar = this.GetProcessbar();
TotalCount = this.GetTotalCount();

//以下判斷原來判斷請求的不同過程,是第一次請求,還是更新進(jìn)度條的請求,實(shí)現(xiàn)方法也可以劃分為多個程序來實(shí)現(xiàn)。
if (Request.QueryString["step"] != null && Request.QueryString["step"].Equals(String.Empty) == false)
{
if (Request.QueryString["step"].Equals("1"))
{
// 開始執(zhí)行任務(wù)的請求,啟動長時間的任務(wù)處理。
Processbar = 0;
//通過計算,得出TotalCount的值,比如查詢數(shù)據(jù)庫等,也可以是一個任務(wù)的多個步驟的總和。
TotalCount = 200; //假如完成一個任務(wù)需要200個步驟
SaveData();
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessTask));
thread.Start();
Response.ClearContent();
Response.Write(0);
Response.End();
}
else
{
Response.ClearContent();
if (Processbar < TotalCount)
{
// 輸出處理的過程
Response.Write(Processbar * 100 / TotalCount);
}
else
{
// 所有的任務(wù)都完成了,輸出結(jié)束信息,終止前端的請求。
Response.Write("ok");
Cache.Remove(key);
}
Response.End();
}
}
else
{
G.Text = key;
if (System.IO.File.Exists(Server.MapPath(key + ".txt")))
{
System.IO.File.Delete(Server.MapPath(key + ".txt"));
}
}
}

/// <summary>
/// 得到執(zhí)行過程的階段
/// </summary>
/// <returns></returns>
private int GetProcessbar()
{
String data = Convert.ToString(Cache.Get(key));
if (String.IsNullOrEmpty(data))
return 0;
else
{
return Convert.ToInt32(data.Split(',')[0]);
}
}

/// <summary>
/// 得到全部的過程數(shù)
/// </summary>
/// <returns></returns>
private int GetTotalCount()
{
String data = Convert.ToString(Cache.Get(key));
if (String.IsNullOrEmpty(data))
return 0;
else
{
return Convert.ToInt32(data.Split(',')[1]);
}
}

/// <summary>
/// 將過程保存。
/// </summary>
private void SaveData()
{
WriteLog();
Cache.Insert(key, Processbar.ToString() + "," + TotalCount.ToString());
}

private void WriteLog()
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath(key + ".txt"), true);
sw.WriteLine("Processbar = " + Processbar + " TotalCount = " + TotalCount + " " + System.DateTime.Now.ToString

());
sw.Close();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml ">
<head id="Head1" runat="server">
<title>在客戶端顯示服務(wù)器端任務(wù)處理進(jìn)度條的探討</title>

<script type="text/Javascript">
var http = null;
var count = 1;
var timer = null;
var guid = "<ASP:Literal id='G' runat='server'/>";
function createXMLHTTP() {
return window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject("MSXML2.XMLHTTP");
}
function showProcess() {
http = createXMLHTTP();
http.open("GET", "<%=Request.Url.ToString() %>?step=" + (count++) + "&guid=" + guid + "&" + Date.parse(new

Date()), true);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200)
if ("ok" == http.responseText) {
document.getElementById("process").innerHTML = document.getElementById("processbar1").innerHTML = "完成";
document.getElementById("processbar2").style.width = "100%";
window.clearInterval(timer);
}
else {
document.getElementById("process").innerHTML = document.getElementById("processbar1").innerHTML =

http.responseText + "%";
document.getElementById("processbar2").style.width = http.responseText + "%";
}
}
http.send(null);
}

function startTask() {
count = 1;
document.getElementById("process").innerHTML = document.getElementById("processbar1").innerHTML = "0%";
document.getElementById("processbar2").style.width = "0%";
timer = window.setInterval("showProcess()", 1000);
return false;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<input type="button" value="啟動處理長時間操作" onclick="return startTask();" />
<div style="border: 1px solid blue; width: 600px; position: relative;margin:10px 0;">
<div style="background: #f00; width: 0; height: 20px;" id="processbar2"></div>
<div style="position: absolute; text-align: center; top: 0; width: 100%" id="processbar1"></div>
</div>
<div id="process"></div>
</form>
</body>
</html>

代碼執(zhí)行效果:
孟憲會 
但是,這種方法就是萬事大吉了嗎?完全錯誤,這種方法仍然存在顯示不準(zhǔn)確的現(xiàn)象,造成顯示不準(zhǔn)確的原因就是 Cache 的使用,IIS 6之后,增加了應(yīng)用程序池的功能,這個功能可以大大提高程序的性能,減少程序本身的錯誤導(dǎo)致的整個網(wǎng)站的崩潰。但是,如果應(yīng)用程序池的“性能”-“Web 園”數(shù)目設(shè)置大于1的情況下,HttpApplicationState(Application)、Cache、HttpSessionState(Session)這些變量都是都是無法使用了,這是因?yàn)椋好總€Web 園會啟動一個w3wp.exe工作進(jìn)程,每個工作進(jìn)程之間是相互獨(dú)立的,以上這些變量也就是不是共享的了,所以,使用Cache保存程序執(zhí)行進(jìn)度的方法也是不完全正確的。

那么終極的方法是什么呢?對,將程序執(zhí)行進(jìn)度保存在第三方的存儲介質(zhì)上,如數(shù)據(jù)庫,文件系統(tǒng)等等都是可以的。這個方法代碼我就不寫了,就是增加訪問數(shù)據(jù)庫的部分即可。

AspNet技術(shù)asp.net 在客戶端顯示服務(wù)器端任務(wù)處理進(jìn)度條的探討,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产色精品久久人妻无码看片 | 老师别揉我胸啊嗯小说 | 日本美女bb| 亚洲一区二区免费看 | 久久成人无码国产免费播放 | 116美女写真午夜电影z | 国产成人无码区免费内射一片色欲 | 忘忧草研究院一二三 | 国产成人自拍视频在线观看 | 一本色道久久综合亚洲精品 | 美国ZOOM动物在线观看 | 熟女人妻-蜜臀AV-首页 | 国产成人小视频 | 蜜桃传媒在线播放 | chinese学生gv video| 精选国产AV精选一区二区三区 | 2021全国精品卡一卡二 | 国产成人教育视频在线观看 | 成人欧美一区二区三区白人 | 欧美含羞草免费观看全部完 | 国产精品视频人人做人人爽 | 国产人妖一区二区 | 翘臀少妇被扒开屁股日出水爆乳 | 在线观看亚洲免费人成网址 | 国产精品高清m3u8在线播放 | 99热这里只就有精品22 | 亚洲精品有码在线观看 | Y8848高清私人影院软件优势 | 一抽一出BGM免费50分动漫 | 18禁三级黄 | 特黄特色大片免费播放器9 特黄特黄aaaa级毛片免费看 | yw193.c国产在线观看 | 亚洲欧美人成视频在线 | 狠狠色狠狠色88综合日日91 | 精品三级在线观看 | 用震蛋调教女性下面视频 | 日韩人妻双飞无码精品久久 | 日日撸影院在线 | BT7086福利二区最新 | 久久伊人男人的天堂网站 | 中文字幕亚洲欧美日韩2o19 |