asp.net線程大量匯入資料時怎麼通過ajax擷取執行狀態

來源:互聯網
上載者:User

前言

最近因為工作中遇到一個需求,需要做了一個大量匯入功能,但長時間運行沒個反饋狀態,很容易讓人看了心急,產生各種臆想!為瞭解決心裡障礙,寫了這麼個功能。

通過線程執行匯入,並把正在執行的狀態存入session,既共用執行狀態,通過ajax調用session裡的執行狀態,從而實現反饋匯入狀態的功能!

上代碼: 前端頁面

 
 代碼如下 複製代碼
<!DOCTYPE html>
<htmllang="en">
<head>
 <metacharset="UTF-8">
 <title>大量匯入資料</title>
 <styletype="text/css">
  .pop_body_con { width: 310px; position: fixed; top: 50%; left: 50%; margin-left: -150px; background: #eee; display:none; }
   .pop_body_con .pop_head { width: auto; padding: 10px 0; background: #fff; }
    .pop_body_con .pop_head a { display: block; color: #717274; font-size: 12px; text-decoration: none; text-align: center; }
  .pop_box { width: auto; overflow: hidden; padding: 45px 10px; }
   .pop_box .pop_text { float: left; }
    .pop_box .pop_text p { padding: 0; margin: 0; font-size: 12px; line-height: 18px; color: #717274;}
   .pop_box .progress_bar_con { float: left; width: 220px; position: relative; z-index: 2; }
    .pop_box .progress_bar_con p { margin: 0; padding: 0; font-size: 12px; color: #fff; line-height: 18px; width: 100%;
            text-align: center; position: absolute; left: 0; top: 0; z-index: 4; }
    .pop_box .progress_bar_con .progress_bar_start { width: 100%; height: 18px; background: #C4C0C0; }
    .pop_box .progress_bar_con .progress_bar_end { width: 16%; height: 18px; background: #2bd35d; position: absolute; left: 0; top: 0; z-index: 3; }
   .pop_box .progress_bar_con { float: left; }
  #loading-mask { width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; z-index: 0; background-color: rgba(0, 0, 0, 0.34902); display: none; }
 </style>
 <scriptsrc="ajax-master/jquery.js"></script>
 <script>
  var MyInterval;
 
  $(function () {
   $("#startImport").click(function () {
    MyInterval = setInterval(getState, 1000);
   });
  });
   
  function getState() {
   $.ajax({
    url: "test.aspx",
    type: "Post",
    data: { action: "getSession" },
    success: function (msg) {
     if (msg != "null") {
      msg = eval('(' + msg + ')');
      if (msg.being == 100) {
       setProcessBar(1, 1);
       $(".pop_body_con").hide();
       $("#loading-mask").hide();
       clearInterval(MyInterval);
      }
      else {
       $(".pop_body_con").show();
       $("#loading-mask").show();
       setProcessBar(msg.being, msg.count)
      }
     }
    }
   });
  }
 
  function setProcessBar(exeFlag, exeMax) {
   $("#progressbar_text").html(parseInt(roundFun(exeFlag / exeMax, 2) * 100) + "%");
   $("#progressbar_bar").attr("style", "width:" + parseInt(roundFun(exeFlag / exeMax, 2) * 100) + "%;");
  }
 
  function roundFun(number, X) {
   X = (!X ? 2 : X);
   return Math.round(number * Math.pow(10, X)) / Math.pow(10, X);
  }
 </script>
</head>
<bodystyle="background-color: #fff;">
 <inputid="startImport"type="button"value="匯入資料"/>
 <divid="loading-mask"></div>
 <divclass="pop_body_con">
  <divclass="pop_head">
   <ahref="javascript:;">正在匯入…請勿操作!</a>
  </div>
  <divclass="pop_box">
   <divclass="pop_text">
    <p>匯入進度:</p>
   </div>
   <divclass="progress_bar_con">
    <pid="progressbar_text">0%</p>
    <divclass="progress_bar_start"></div>
    <divclass="progress_bar_end"id="progressbar_bar"></div>
   </div>
  </div>
 </div>
</body>
</html>
 

後台頁面:

 
 代碼如下 複製代碼
usingSystem.Linq;
usingSystem.Threading;
usingSystem.Web;
usingSystem.Web.Script.Serialization;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
 
publicpartialclasstest : System.Web.UI.Page
{
 protectedvoidPage_Load(objectsender, EventArgs e)
 {
  stringaction = Request.Form["action"];
  if(!string.IsNullOrEmpty(action))
  {
   Hashtable temp = tmethod();
   if(temp ==null)
   {
    Thread trd =newThread(newParameterizedThreadStart(insertData));
    trd.Start(action);
   }
   else
   {
    if(temp["reCode"].ToString() =="100")
    {
      
     Session.Remove("process");
    }
   }
 
   JavaScriptSerializer ser =newJavaScriptSerializer();
   String jsonStr = ser.Serialize(temp);
   Response.Write(jsonStr);
   Response.End();
  }
 }
 
 
 publicHashtable tmethod()
 {
  return(Hashtable)Session["process"];
 }
 
 privatevoidinsertData(objectobj)
 {
  stringaction = obj.ToString();
  inttCount = 100;
  for(inti = 0; i < tCount; i++)
  {
   Hashtable stateHash = setStateVal(0, i, tCount, action);
   Session["process"] = stateHash;//存入session,方便共用執行狀態
   Thread.Sleep(500);
  }
  Session["process"] = setStateVal(100, tCount, tCount, action);
  Thread.CurrentThread.Abort();
 }
 
 privateHashtable setStateVal(intcode,intbeingV,intCountV,stringaction)
 {
  Hashtable stateHash =newHashtable();
  stateHash["reCode"] = code;//返回狀態值
  stateHash["being"] = beingV; //正在執行值
  stateHash["count"] = CountV; //總值
  stateHash["action"] = action; //總值
  returnstateHash;
 }
}
 

ok,共用完畢!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.