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

通过线程执行导入,并把正在执行的状态存入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; 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="">
 <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>
 
后台
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;
 }
}

asp.net线程批量导入数据时通过ajax获取执行状态的更多相关文章

  1. asp.net 线程批量导入数据,ajax获取执行状态

    最近做了一个批量导入功能,长时间运行,没个反馈状态,很容易让人看了心急,产生各种臆想!为了解决心里障碍,写了这么个功能. 通过线程执行导入,并把正在执行的状态存入session,既共享执行状态,通过a ...

  2. SQLSERVER:大容量导入数据时保留标识值 (SQL Server)

    从MSDN上看到实现大容量导入数据时保留标识值得方法包含三种: MSDN链接地址为:https://msdn.microsoft.com/zh-cn/library/ms178129.aspx 感觉M ...

  3. Cassandra使用pycassa批量导入数据

    本周接手了一个Cassandra系统的维护工作,有一项是需要将应用方的数据导入我们维护的Cassandra集群,并且为应用方提供HTTP的方式访问服务.这是我第一次接触KV系统,原来只是走马观花似的看 ...

  4. Redis批量导入数据的方法

    有时候,我们需要给redis库中插入大量的数据,如做性能测试前的准备数据.遇到这种情况时,偶尔可能也会懵逼一下,这里就给大家介绍一个批量导入数据的方法. 先准备一个redis protocol的文件( ...

  5. 使用BCP批量导入数据

    本文原创,转载请标明出处 BCP 工具的使用 The bulk copy program utility (bcp) bulk copies data between an instance of M ...

  6. neo4j批量导入数据的两种解决方案

    neo4j批量导入数据有两种方法,第一种是使用cypher语法中的LOAD CSV,第二种是使用neo4j自带的工具neo4j-admin import. LOAD CSV 导入的文件必须是csv文件 ...

  7. SQL Server导入数据时“启用标示插入”详解

    在SQL Server中导入数据时,会有一个"启用标示插入"的选项,突然间懵逼了,这到底啥意思?我选与不选这个选项,结果好像没区别!不科学啊这,"存在即合理", ...

  8. csv文件批量导入数据到sqlite。

    csv文件批量导入数据到sqlite. 代码: f = web.input(bs_switch = {})  # bs_switch 为from表单file字段的namedata =[i.split( ...

  9. [MySQL]load data local infile向MySQL数据库中导入数据时,无法导入和字段不分离问题。

    利用load data将文件中的数据导入数据库表中的时候,遇到了两个问题. 首先是load data命令无法执行的问题: 命令行下输入load data local infile "path ...

随机推荐

  1. OOM问题定位方法

    1. 背景 线上内存OOM问题是最难定位的问题,最常见的原因: (1)本身资源不够 (2)申请的太多 (3)资源耗尽 某服务器上部署了Java服务,出现OutOfMemoryError,请问有可能是什 ...

  2. 日期时间函数(1)-time()&gmtime()&strftime()&localtime()

    ◆time() 取得当前时间.此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数.如果参数t为非空指针的话, 此函数也会将返回值存到t指针所指的内存. 成功则返回秒数 ...

  3. Set 集合论

    https://en.m.wikipedia.org/wiki/De_Morgan%27s_laws https://plato.stanford.edu/entries/set-theory/ ht ...

  4. vue-cli脚手架里如何配置屏幕自适应

    很多同学可能在写h5的时候,也会遇到移动端如何控制屏幕自适应问题!在移动端网页开发中,我们可以用手机淘宝的flexible.那么在vue当中,也同样可以用!接下来就介绍下如何在vue-cli配置的项目 ...

  5. configure new Linux/Mac

    vim   http://www.cnblogs.com/wswang/p/5088078.html sublime   http://jingyan.baidu.com/article/3aed63 ...

  6. Spring Boot干货系列:(三)启动原理解析

    Spring Boot干货系列:(三)启动原理解析 2017-03-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章我们见识了SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说 ...

  7. Excel VBA 操作 Word(入门篇)

    原文地址 本文的对象是:有一定Excel VBA基础,对Word VBA还没有什么认识,想在Excel中通过VBA操作Word还有困难的人.   一.新建Word引用 需要首先创建一个对 Word A ...

  8. C++控制台读取和输出函数

    c中puts()函数用来向标准输出设备(屏幕)写字符串并换行,其调用方式为,puts(s);其中s为字符串字符(字符串数组名或字符串指针). 功 能: 送一字符串到流stdout中 用 法: int ...

  9. CenOS6.5下源码安装vim-7.4

    1.[下载] vim-7.4下载地址: ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2 2.[解压] tar jxvf vim-7.4.tar.bz2 之 ...

  10. USB 之传输编码格式 NRZI 介绍

    记录NRZI (Non-Return-to-Zero Inerted code) 非归零翻转编码,之前,我先稍微记录一下他的前身. RZ 编码(Return- to - zero coding) RZ ...