The execution status is obtained through ajax when the asp.net thread imports data in batches.

Source: Internet
Author: User
Tags net thread recode

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.

Related Article

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.