How do I get execution status through Ajax when ASP.net threads bulk Import data

Source: Internet
Author: User

Objective

Recently because of the work encountered a demand, need to do a bulk import function, but a long time to run no feedback status, it is easy to see the impatient, produce all kinds of conjecture! In order to solve the heart barrier, wrote this function.

Through the thread to execute the import, and put the state of execution into session, both share the execution state, through the AJAX call session execution status, so as to achieve feedback import state function!

On Code: Front page

The code is as follows Copy Code
<! DOCTYPE html>
<metacharset= "UTF-8" >
<title> Bulk Import Data </title>
<styletype= "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>
<scriptsrc= "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 (x))/Math.pow (x);
}
</script>
<bodystyle= "Background-color: #fff;" >
<inputid= "Startimport" type= "button" value= "Import data"/>
<divid= "Loading-mask" ></div>
<divclass= "Pop_body_con" >
<divclass= "Pop_head" >
<ahref= "javascript:;" > Importing ... Please do not operate! </a>
</div>
<divclass= "Pop_box" >
<divclass= "Pop_text" >
<p> Import Progress:</p>
</div>
<divclass= "Progress_bar_con" >
<pid= "Progressbar_text" >0%</p>
<divclass= "Progress_bar_start" ></div>
<divclass= "Progress_bar_end" id= "Progressbar_bar" ></div>
</div>
</div>
</div>
</body>

Background page:

The code is as follows Copy Code
Usingsystem.linq;
usingsystem.threading;
Usingsystem.web;
UsingSystem.Web.Script.Serialization;
UsingSystem.Web.UI;
UsingSystem.Web.UI.WebControls;
Publicpartialclasstest:System.Web.UI.Page
{
Protectedvoidpage_load (Objectsender, EventArgs e)
{
Stringaction = request.form["Action"];
if (!string. IsNullOrEmpty (Action))
{
Hashtable temp = TMethod ();
if (temp ==null)
{
Thread TRD =newthread (Newparameterizedthreadstart (InsertData));
Trd. Start (action);
}
Else
{
if (temp["Recode"]. ToString () = = "100")
{
Session.remove ("process");
}
}
JavaScriptSerializer ser =newjavascriptserializer ();
String jsonstr = ser. Serialize (temp);
Response.Write (JSONSTR);
Response.End ();
}
}
Publichashtable TMethod ()
{
Return (Hashtable) session["process"];
}
Privatevoidinsertdata (Objectobj)
{
Stringaction = obj. ToString ();
Inttcount = 100;
for (inti = 0; i < Tcount; i++)
{
Hashtable Statehash = setstateval (0, I, tcount, action);
session["process"] = statehash;//into session to facilitate shared execution status
Thread.Sleep (500);
}
session["process"] = Setstateval (M, Tcount, Tcount, action);
Thread.CurrentThread.Abort ();
}
Privatehashtable Setstateval (intcode,intbeingv,intcountv,stringaction)
{
Hashtable Statehash =newhashtable ();
statehash["Recode"] = code;//return status value
Statehash["being"] = BEINGV; Executing value
statehash["Count" = Countv; Worth
statehash["Action" = action; Worth
Returnstatehash;
}
}

OK, sharing is complete!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.