The execution status is obtained through ajax when the asp.net thread imports data in batches.
Preface
Recently, I encountered a requirement in my work and needed to implement a batch import function. However, there was no feedback during a long period of operation. It was easy for people to be impatient and have various ideas! In order to solve the mental obstacle, I wrote such a function.
Execute the import through a thread and save the execution status to the session. This allows you to share the execution status and call the execution status in the session through ajax to feedback the import status!
Code above: front-end page
<! DOCTYPE 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; // save it to the session to facilitate sharing of the execution status 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; // The value of stateHash being executed ["count"] = CountV; // total value stateHash ["action"] = action; // return stateHash ;}}
OK. Sharing is complete!
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.