(Original) ASP.NET uses multi-thread execution for a long time task, the client shows an example of the implementation progress

xiaoxiao2021-03-05  25

Perform a long time in ASP.NET, sometimes you need to have a feedback on the client to understand the progress of the task, roughly see how many practices: (1) Press the button to give Out of a

prompt is performing a task, performing the
hide (2) When the button is pressed, jump to a prompt task being executed, and then jump back (3) to make a task Class, turn on another thread execution task, and save this class's instance to track the implementation of the task on the client or server side

(1) and (2) are more useful, but also relatively simple, the disadvantage is that the progress of the task cannot be real-time, and the time may time out, (3) is better to solve the above 2 disadvantages. The following focuses on the implementation method of (3), first from the simple start, we do a task class, when the client (the transfer time is 1 second) know how much time executing, and given successfully after successful completion tasks The execution time gives an error time at the task error.

Front desk


Backstage

First, some classes: protected system.web.ui.webcontrols.button btn_startwork; protected system.web.ui.webcontrols.Labl lab_state; // front 2 is VS.NET you generated by protected Work W;

Enter the following code in Page_Load:

IF (session ["Work" == null) {w = new work (); session ["Work" = W;} else {w = (work) session ["Work"];} Switch (W.State ) {Case 0: {this.Lab_state.text = "There is no task yet"; Break;} case 1: {this.Lab_State.Text = "Task" (TimeSpan) (DateTime.now-W.StartTime )))). Totalseconds "second"; this.btn_startwork.enabled = false; page.registerStartupScript (","