When performing a long operation in ASP. NET, you may need to have a feedback on the progress of the task on the client. You can roughly see the following methods:
1) when you press the button, a <div> prompt is displayed, indicating that the task is being executed. After the task is executed, hide the <div>
2) When you press the button, you will be redirected to a page indicating that the task is being executed. After the task is executed, you will be redirected back.
3) create a task class, enable another thread to execute the task, and save the instance of this class on the client or server to track the task execution 1) and 2) it is also relatively simple. The disadvantage is that you cannot know the progress of the task in real time, and a long time may time out. 3) the above two shortcomings will be better solved. Next, we will focus on the 3) implementation method. First, we will create a task class, which will be refreshed for 1 second at the client time) to learn how long the task has been executed, the execution time is displayed after the task is successfully completed, and the error time is displayed when the task fails.
ASP. NET multi-thread foreground
- <Form Id="Form1" Method="Post" Runat="Server">
- <Asp: label Id="Lab_state" Runat="Server"></Asp: label><Br>
- <Asp: Button Id="Btn_startwork" Runat="Server" Text="Running a long task"></Asp: Button>
- </Form>
ASP. NET multi-thread background
First, some class declarations:
- Protected System. Web. UI. WebControls. Button btn_startwork;
- Protected System. Web. UI. WebControls. Label lab_state;
- // The first two are self-generated by vs.net.
- Protected work w;
- Enter the following code in Page_Load:
- If (Session ["work"] = null)
- {
- W=NewWork ();
- Session ["work"] = w;
- }
- Else
- {
- W= (Work) Session ["work"];
- }
- Switch (w. State)
- {
- Case 0:
- {
- This. lab_state.Text="Task not started";
- Break;
- }
- Case 1:
- {
- This. lab_state.Text="The task has been executed"+ (TimeSpan) (DateTime. Now-w.StartTime). TotalSeconds + "seconds ";
- This. btn_startwork.Enabled=False;
- Page. RegisterStartupScript ("","<Script>Window. setTimeout ('LocationLocation. href= Location. href ', 1000 );</Script>");
- // Constantly refresh this page and update the task status at any time
- Break;
- }
- Case 2:
- {
- This. lab_state.Text="When the task ends and all operations are successfully performed"+ (TimeSpan) (w. FinishTime-w.StartTime). TotalSeconds + "seconds ";
- This. btn_startwork.Enabled=True;
- Break;
- }
- Case 3:
- {
- This. lab_state.Text="The task is finished. In"+ (TimeSpan) (w. ErrorTime-w.StartTime). TotalSeconds + "an error occurs in seconds, causing task failure ";
- This. btn_startwork.Enabled=True;
- Break;
- }
- }
Enter the following code in the button-click event:
- If (w. State! = 1)
- {
- This. btn_startwork.Enabled=False;
- W. runwork ();
- Page. RegisterStartupScript ("","<Script>LocationLocation. href= Location. href;</Script>");
- // Refresh the page immediately
- }
Create a task class with the following code:
- Public class work
- {
- Public intState=0; // 0-no start, 1-running, 2-success, 3-Failure
- Public DateTime StartTime;
- Public DateTime FinishTime;
- Public DateTime ErrorTime;
- Public void runwork ()
- {
- Lock (this) // ensure that the critical section is occupied by a Thread
- {
- If (State! = 1)
- {
- State=1;
- StartTime=DateTime. Now;
- System. Threading. ThreadThread=NewSystem. Threading. Thread (new System. Threading. ThreadStart (dowork ));
- Thread. Start ();
- }
- }
- }
- Private void dowork ()
- {
- Try
- {
- SqlConnectionConn=NewSqlConnection (System. Configuration. ConfigurationSettings. receivettings ["conn"]);
- SqlCommandCmd=NewSqlCommand ("Insert Into test (test) values ('test')", conn );
- Conn. Open ();
- For (intI=0; I<5000; I ++) cmd. ExecuteNonQuery ();
- Conn. Close ();
- // Execute a time-consuming database operation with the above Code
- State=2;
- }
- Catch
- {
- ErrorTime=DateTime. Now;
- State=3;
- }
- Finally
- {
- FinishTime=DateTime. Now;
- }
- }
- }
- }
Run this page and you will see the time when the feedback task is executed every second. The total time of the task is given after the end. If a task error occurs, the error time is also displayed)
This ASP. the. NET multi-thread example is relatively simple. It can basically implement the interaction between long-time task execution and the client, but the interface is not very friendly. If there are many operations, it can only show how long it has been executed, the number of tasks executed cannot be displayed. In the next article, this class and interface will be improved)
- DataList and Repeater controls of ASP. NET
- Analysis of IIS ing in ASP. NET
- Overview ASP. NET status types
- Introduction to ASP. NET and Web servers
- EnableViewState attribute of ASP. NET