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

首頁 > 編程 > .NET > 正文

asp.net線程批量導入數據時通過ajax獲取執行狀態

2024-07-10 13:31:51
字體:
來源:轉載
供稿:網友

前言

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

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

上代碼: 前端頁面

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>批量導入數據</title> <style type="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> <script src="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><body style="background-color: #fff;"> <input id="startImport" type="button" value="導入數據" /> <div id="loading-mask" ></div> <div class="pop_body_con">  <div class="pop_head">   <a href="javascript:;">正在導入…請勿操作!</a>  </div>  <div class="pop_box">   <div class="pop_text">    <p>導入進度:</p>   </div>   <div class="progress_bar_con">    <p id="progressbar_text">0%</p>    <div class="progress_bar_start"></div>    <div class="progress_bar_end" id="progressbar_bar"></div>   </div>  </div> </div></body></html>

后臺頁面:

using System.Linq;using System.Threading;using System.Web;using System.Web.Script.Serialization;using System.Web.UI;using System.Web.UI.WebControls;public partial class test : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) {  string action = Request.Form["action"];  if (!string.IsNullOrEmpty(action))  {   Hashtable temp = tmethod();   if (temp == null)   {    Thread trd = new Thread(new ParameterizedThreadStart(insertData));    trd.Start(action);   }   else   {    if (temp["reCode"].ToString() == "100")    {          Session.Remove("process");    }   }   JavaScriptSerializer ser = new JavaScriptSerializer();   String jsonStr = ser.Serialize(temp);   Response.Write(jsonStr);   Response.End();  } } public Hashtable tmethod() {  return (Hashtable)Session["process"]; } private void insertData(object obj) {  string action = obj.ToString();  int tCount = 100;  for (int i = 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(); } private Hashtable setStateVal(int code, int beingV, int CountV, string action) {  Hashtable stateHash = new Hashtable();  stateHash["reCode"] = code; //返回狀態值  stateHash["being"] = beingV;  //正在執行值  stateHash["count"] = CountV;  //總值  stateHash["action"] = action;  //總值  return stateHash; }}

ok,共享完畢!

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。


注:相關教程知識閱讀請移步到ASP.NET教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武穴市| 海盐县| 竹北市| 桐乡市| 张家口市| 阆中市| 济源市| 竹北市| 锡林郭勒盟| 三穗县| 秭归县| 玉林市| 汤阴县| 内乡县| 武平县| 铁力市| 阿图什市| 太保市| 上饶市| 铜陵市| 娄烦县| 章丘市| 石狮市| 乌鲁木齐县| 察雅县| 张家港市| 墨脱县| 祁连县| 中超| 佛教| 时尚| 横山县| 遵义县| 大关县| 镇康县| 永济市| 威海市| 昭平县| 昭苏县| 平舆县| 微山县|