asp.net利用多线程执行长时间的任务,客户端显示出任务的执行进度的示例(二)


所属类别:.NET

文章作者:佚名

特别推荐:免费发布信息 承包关键词~~抢爆了!HOT!


http://blog.csdn.net/LoveCherry/archive/2005/04/10/342078.aspx对上一次的做一点修改,增加一个比较美观的进度显示上面那个是运行中的画面,下面那个是结束后的画面用到的图标在这里:对上次的前台修改如下:<%@ Page language="c#" Codebehind="WebForm54.aspx.cs" AutoEventWireup="false" Inherits="csdn.WebForm54" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >WebForm54.font { FONT-WEIGHT: normal; FONT-SIZE: 9pt; COLOR: #000000; FONT-FAMILY: "宋体", sans-serif; BACKGROUND-COLOR: #f0f0f0; TEXT-DECORATION: none }后台修改如下: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{/// /// WebForm54 的摘要说明。/// 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("","window.setTimeout('location.href=location.href',1000);");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);}/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// private void InitializeComponent(){this.btn_startwork.Click += new System.EventHandler(this.btn_startwork_Click);this.Load += new System.EventHandler(this.Page_Load);}#endregionprivate 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("","location.href=location.href;");}}}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;}}}} 关闭本页

相关信息

· asp批量修改记录的代码

· PHP的面向对象编程:开发大型PHP项目的方法(二)(转载)

· 两种与SQL Server数据库交换数据的方法

· 聆听天籁之声 详尽剖析Vista音量种类








....

9331 40206