This article is a continuation of ASP. NET's multi-thread programming practice topic. It will improve the multi-thread class and interface in the example of ASP. NET's use of multi-thread long-time tasks. I hope programmers can provide valuable suggestions.
The last modification adds a more beautiful progress display.
The picture above is running, and the picture below is after the end
Here are the icons used:
The last front-end modification is as follows:
<% @ Page Language = "C #" codebehind = "webform54.aspx. cs" autoeventwireup = "false" inherits = "csdn. webform54" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform54 </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetschema">
<Style type = "text/CSS">
. Font {font-weight: normal; font-size: 9pt; color: #000000; font-family: "", sans-serif; Background-color: # f0f0f0; text-Decoration: None}
</Style>
</Head>
<Body>
<Form ID = "form1" method = "Post" runat = "server">
<Div id = "div_load" runat = "server">
<Table width = "320" Height = "72" border = "1" bordercolor = "# cccccc" cellpadding = "5" cellspacing = "1"
Class = "font" style = "filter: alpha (opacity = 80); width: 320px; Height: 72px">
<Tr>
<TD>
<P>
<Br>
<Asp: Label id = "lab_state" runat = "server"> </ASP: Label> </P>
</TD>
</Tr>
</Table>
<Br>
</Div>
<Asp: button id = "btn_startwork" runat = "server" text = "running a long task"> </ASP: button> <br>
<Br>
<Asp: Label id = "lab_jg" runat = "server"> </ASP: Label>
</Form>
</Body>
</Html>
The background modification is as follows:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace csdn
{
/// <Summary>
/// Summary of webform54.
/// </Summary>
Public class webform54: system. Web. UI. Page
{
Protected system. Web. UI. htmlcontrols. htmlgenericcontrol div_load;
Protected system. Web. UI. webcontrols. Button btn_startwork;
Protected system. Web. UI. webcontrols. Label lab_state;
Protected system. Web. UI. webcontrols. Label lab_jg;
Protected work W;
Private void page_load (Object sender, system. eventargs E)
{
// Place the user here Code To initialize the page
If (session ["work"] = NULL)
{
W = new work ();
Session ["work"] = W;
}
Else
{
W = (work) session ["work"];
}
Switch (W. State)
{
Case 0:
{
This. div_load.visible = false;
Break;
}
Case 1:
{
This. lab_state.text = "" + (timespan) (datetime. now-w.StartTime )). totalseconds. tostring ("0.00") + "seconds passed, completion percentage:" + W. percent + "% ";
This. btn_startwork.enabled = false;
Page. registerstartupscript ("", "<SCRIPT> window. setTimeout ('location. href = location. href ', 1000); </SCRIPT> ");
This. lab_jg.text = "";
Break;
}
Case 2:
{
This. lab_jg.text = "the task is finished and all operations are successfully performed, with" + (timespan) (W. FinishTime-w.StartTime). totalseconds + "seconds ";
This. btn_startwork.enabled = true;
This. div_load.visible = false;
Break;
}
Case 3:
{
This. lab_jg.text = "task ended, with an error in" + (timespan) (W. ErrorTime-w.StartTime). totalseconds + "seconds leading to task failure '";
This. btn_startwork.enabled = true;
This. div_load.visible = false;
Break;
}
}
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. btn_startwork.click + = new system. eventhandler (this. btn_startwork_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void btn_startwork_click (Object sender, system. eventargs E)
{
If (W. State! = 1)
{
This. btn_startwork.enabled = false;
This. div_load.visible = true;
W. runwork ();
Page. registerstartupscript ("", "<SCRIPT> location. href = location. href; </SCRIPT> ");
}
}
}
Public class work
{
Public int state = 0; // 0-not started, 1-running, 2-successful, 3-failed
Public int percent = 0; // percentage of completion
Public datetime starttime;
Public datetime finishtime;
Public datetime errortime;
Public void runwork ()
{
Lock (this)
{
If (State! = 1)
{
State = 1;
Starttime = datetime. now;
System. Threading. Thread thread = new system. Threading. Thread (new system. Threading. threadstart (dowork ));
Thread. Start ();
}
}
}
Private void dowork ()
{
Try
{
Sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["conn"]);
Sqlcommand cmd = new sqlcommand ("insert into test (test) values ('test')", Conn );
Conn. open ();
For (INT p = 0; P <100; P ++)
{
For (INT I = 0; I <10; I ++)
{
Cmd. executenonquery ();
}
Percent = P; // The percentage is defined here. You can estimate how much time the operation fee is defined as a percentage.
}
Conn. Close ();
// Execute a time-consuming database operation with the above Code
State = 2;
}
Catch
{
Errortime = datetime. now;
Percent = 0;
State = 3;
}
Finally
{
Finishtime = datetime. now;
Percent = 0;
}
}
}
}
Reference address: http://www.cnblogs.com/lovecherry/archive/2005/04/10/135090.html