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

前言

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

通过线程执行导入,并把正在执行的状态存入session,既共享执行状态,通过ajax调用session里的执行状态,从而实现反馈导入状态的功能!

上代码: 前端页面

批量导入数据

.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; }

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);

}

正在导入…请勿操作!

导入进度:

0%

后台页面:

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,共享完毕!

总结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值