国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net利用多線程執(zhí)行長時間的任務(wù),客戶端顯示出任務(wù)的執(zhí)行進度的示例(二)

2024-07-10 12:56:39
字體:
供稿:網(wǎng)友
對上一次的做一點修改,增加一個比較美觀的進度顯示


上面那個是運行中的畫面,下面那個是結(jié)束后的畫面


用到的圖標(biāo)在這里:

對上次的前臺修改如下:

<%@ 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" >
     <tr>
      <td>
       <p><img alt="請等待" src="http://www.163design.net/n/a/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="運行一個長時間的任務(wù)"></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="任務(wù)結(jié)束,并且成功執(zhí)行所有操作,用時 "+((timespan)(w.finishtime-w.starttime)).totalseconds+" 秒";
     this.btn_startwork.enabled=true;
     this.div_load.visible=false;
     break;
    }
    case 3:
    {
     this.lab_jg.text="任務(wù)結(jié)束,在"+((timespan)(w.errortime-w.starttime)).totalseconds+"秒的時候發(fā)生錯誤導(dǎo)致任務(wù)失敗’";
     this.btn_startwork.enabled=true;
     this.div_load.visible=false;
     break;
    }
   }
  }
  #region web 窗體設(shè)計器生成的代碼
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 該調(diào)用是 asp.net web 窗體設(shè)計器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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-成功結(jié)束,3-失敗結(jié)束
  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();
    //以上代碼執(zhí)行一個比較消耗時間的數(shù)據(jù)庫操作
    state=2;
   }
   catch
   {
    errortime=datetime.now;
    percent=0;
    state=3;
   }
   finally
   {
    finishtime=datetime.now;
    percent=0;
   }
  }
 }
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 论坛| 疏勒县| 手游| 哈密市| 三原县| 遵义县| 东阿县| 土默特右旗| 双城市| 马山县| 石景山区| 岫岩| 玛沁县| 沛县| 常山县| 舟山市| 册亨县| 乌兰浩特市| 清丰县| 潼南县| 波密县| 长垣县| 巴楚县| 且末县| 元朗区| 佛山市| 阜康市| 两当县| 阳泉市| 都昌县| 九江县| 宿州市| 绥中县| 交城县| 金平| 桐乡市| 古浪县| 天台县| 旺苍县| 图木舒克市| 福贡县|