一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - asp.net線程批量導入數據時通過ajax獲取執行狀態

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

2020-04-15 13:16LI小白 ASP.NET教程

asp.net線程批量導入數據是大家日常工作中常遇到的一個要求,但批量添加時間一般較長,如果能返回執行的狀態就好,那么下面這篇文章主要給大家介紹了asp.net線程批量導入數據時通過ajax獲取執行狀態的方法,有需要的朋友可以參

前言

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

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

上代碼: 前端頁面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!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>

后臺頁面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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,共享完畢!

總結

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

原文鏈接:http://www.cnblogs.com/LittleBai/p/6179183.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 乳女教师欲乱动漫无修版动画3d | 国产亚洲精品一区二区在线观看 | 国产成+人+亚洲+欧美综合 | 99在线精品免费视频九九视 | 免费人成网址在线观看国内 | 免费观看欧美性一级 | haodiaocao几万部精彩视频 | 91免费精品国自产拍在线可以看 | 免费的毛片视频 | 成人男女啪啪免费观看网站 | 色亚洲色图 | 婷婷综合久久 | 91制片厂制作果冻传媒2021 | 男人视频网站 | 无套白浆 | 亚洲区精品 | 久久人妻少妇嫩草AV无码 | 精品国产线拍大陆久久尤物 | 久久re热在线视频精69 | 牧教师 | 99精品视频一区在线观看miya | 日韩中文在线 | 免费观看美女被cao视频 | 久久伊人在| 欧美日韩综合网在线观看 | 国产成人无精品久久久 | 国内自拍成人网在线视频 | 国产午夜成人无码免费看 | 国产成人高清精品免费5388密 | 偷偷操不一样 | 国产欧美日韩精品一区二 | 性欧美高清强烈性视频 | 亚洲欧美国产自拍 | 无码精品一区二区三区免费视频 | 国产精品一区二区三区久久 | 精品视频入口 | 美女沟厕撒尿全过程高清图片 | 五月天精品视频在线观看 | 亚洲激情视频在线 | 美女脱了内裤让男桶爽 | 亚洲精品久久啪啪网站成年 |