对上一次的做一点修改,增加一个比较美观的进度显示
上面那个是运行中的画面,下面那个是结束后的画面
用到的图标在这里:
对上次的前台修改如下:
<%@ page language=”c#” codebehind=”webform54.aspx.cs” autoeventwireup=”false” inherits=”csdn.webform54″ %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
<head>
<title>webform54</title>
<meta content=”microsoft visual studio .net 7.1″ name=”generator”>
<meta content=”c#” name=”code_language”>
<meta content=”javascript” name=”vs_defaultclientscript”>
<meta content=”http://schemas.microsoft.com/intellisense/ie5″ name=”vs_targetschema”>
<style type=”text/css”>
.font { font-weight: normal; font-size: 9pt; color: #000000; font-family: “宋体”, sans-serif; background-color: #f0f0f0; text-decoration: none }
</style>
</head>
<body>
<form id=”form1″ method=”post” runat=”server”>
<div id=”div_load” runat=”server”>
<table width=”320″ height=”72″ border=”1″ bordercolor=”#cccccc” cellpadding=”5″ cellspacing=”1″
class=”font” style=”filter: alpha(opacity=80); width: 320px; height: 72px”>
<tr>
<td>
<p><img alt=”请等待” src=”clocks.gif” align=”left”>
<br>
<asp:label id=”lab_state” runat=”server”></asp:label></p>
</td>
</tr>
</table>
<br>
</div>
<asp:button id=”btn_startwork” runat=”server” text=”运行一个长时间的任务”></asp:button><br>
<br>
<asp:label id=”lab_jg” runat=”server”></asp:label>
</form>
</body>
</html>
后台修改如下:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace csdn
{
/// <summary>
/// webform54 的摘要说明。
/// </summary>
public class webform54 : system.web.ui.page
{
protected system.web.ui.htmlcontrols.htmlgenericcontrol div_load;
protected system.web.ui.webcontrols.button btn_startwork;
protected system.web.ui.webcontrols.label lab_state;
protected system.web.ui.webcontrols.label lab_jg;
protected work w;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
if(session[“work”]==null)
{
w=new work();
session[“work”]=w;
}
else
{
w=(work)session[“work”];
}
switch(w.state)
{
case 0:
{
this.div_load.visible=false;
break;
}
case 1:
{
this.lab_state.text=””+((timespan)(datetime.now-w.starttime)).totalseconds.tostring(“0.00″)+” 秒过去了,完成百分比:”+w.percent+” %”;
this.btn_startwork.enabled=false;
page.registerstartupscript(“”,”<script>window.settimeout(’location.href=location.href’,1000);</script>”);
this.lab_jg.text=””;
break;
}
case 2:
{
this.lab_jg.text=”任务结束,并且成功执行所有操作,用时 “+((timespan)(w.finishtime-w.starttime)).totalseconds+” 秒”;
this.btn_startwork.enabled=true;
this.div_load.visible=false;
break;
}
case 3:
{
this.lab_jg.text=”任务结束,在”+((timespan)(w.errortime-w.starttime)).totalseconds+”秒的时候发生错误导致任务失败’”;
this.btn_startwork.enabled=true;
this.div_load.visible=false;
break;
}
}
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.btn_startwork.click += new system.eventhandler(this.btn_startwork_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void btn_startwork_click(object sender, system.eventargs e)
{
if(w.state!=1)
{
this.btn_startwork.enabled=false;
this.div_load.visible=true;
w.runwork();
page.registerstartupscript(“”,”<script>location.href=location.href;</script>”);
}
}
}
public class work
{
public int state=0;//0-没有开始,1-正在运行,2-成功结束,3-失败结束
public int percent=0;//完成百分比
public datetime starttime;
public datetime finishtime;
public datetime errortime;
public void runwork()
{
lock(this)
{
if(state!=1)
{
state=1;
starttime=datetime.now;
system.threading.thread thread=new system.threading.thread(new system.threading.threadstart(dowork));
thread.start();
}
}
}
private void dowork()
{
try
{
sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“conn”]);
sqlcommand cmd=new sqlcommand(“insert into test (test)values(’test’)”,conn);
conn.open();
for(int p=0;p<100;p++)
{
for(int i=0;i<10;i++)
{
cmd.executenonquery();
}
percent=p;//这里就是定义百分比,你估计这个操作费多少时间定义多少百分比
}
conn.close();
//以上代码执行一个比较消耗时间的数据库操作
state=2;
}
catch
{
errortime=datetime.now;
percent=0;
state=3;
}
finally
{
finishtime=datetime.now;
percent=0;
}
}
}
}