asp. net thread obtains execution status through ajax when batch importing data

  • 2021-09-04 23:55:57
  • OfStack

Preface

Recently, because I encountered a demand in my work, I need to do a batch import function, but there is no feedback status after running for a long time, which makes people look impatient and produce various assumptions! In order to solve the mental disorder, I wrote this function.

Through the thread implementation of the import, and the state of being executed in session, both sharing the implementation of the state, through ajax call session implementation of the state, so as to achieve feedback into the state of the function!

On code: front page


<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title> Import data in batches </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=" Import data " />
 <div id="loading-mask" ></div>
 <div class="pop_body_con">
  <div class="pop_head">
   <a href="javascript:;"> Importing … Do not operate! </a>
  </div>
  <div class="pop_box">
   <div class="pop_text">
    <p> Import progress: </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>

Background page:


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; // Deposit session To facilitate sharing execution state 
   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; // Return status value 
  stateHash["being"] = beingV;  // Executing value 
  stateHash["count"] = CountV;  // Total value 
  stateHash["action"] = action;  // Total value 
  return stateHash;
 }
}

ok, share complete!

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: