当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net线程批量导入数据时通过ajax获取执行状态

asp.net线程批量导入数据时通过ajax获取执行状态

2017年12月12日  | 移动技术网IT编程  | 我要评论

神武夏日吉祥怎么玩,孕妇失眠正常吗,tc轴承

前言

最近因为工作中遇到一个需求,需要做了一个批量导入功能,但长时间运行没个反馈状态,很容易让人看了心急,产生各种臆想!为了解决心里障碍,写了这么个功能。

通过线程执行导入,并把正在执行的状态存入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,共享完毕!

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网