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

xiaoxiao2021-03-05  22

http://blog.9cbs.net/loveCherry/archive/2005/04/10/342078.aspx to make a little modification of the last time, add a relatively beautiful progress to the screen above, the following is the end The icon for the post-picture is here: the last front desk is modified as follows: <% @ page language = "c #" codebehind = "Webform54.aspx.cs" autoeventwireup = "false" inherits = "9cbs.webform54"%>%>%>%> 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: "Song", 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 = "filt ER: Alpha (Opacity = 80); Width: 320px; Height: 72px "> <Tr> <TD> <P> <img alt =" Please wait "SRC =" clocks.gif "align =" left "> <br > <Asp: label id = "lab_state" runat = "server"></p> <p></ asp: label> </ p> </ td> </ tr> </ table> <br> </ div> <asp: button id = "btn_startwork" runat = "server" text = "running a long time mission "> </ asp: Button> <BR> <BR> <asp: Label id =" lab_jg "runat =" server "> </ asp: Label> </ form> </ body> </ HTML> background modified 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; use system.web.ui.webcontrols; using system.web.ui.htmlcontrols;</p> <p>Namespace 9cbs {/// <summary> /// WebFORM54 summary description. /// </ 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.labl lab_jg; protected workw;</p> <p>Private void Page_load (Object sender, system.eventargs e) {// Place user code here to initialize 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)). Totalseconds.toString ("0.00") "seconds, complete percentage:" w.Percent "%"; this.btn_startwork.enable = false; page.registerStartupScript ("," <script> window.settimeout ('location.href = location.href', 1000); </ script>); this.Lab_jg.text = ""; Case 2: {this.Lab_jg.text = "The task ends, and successfully executes all operations," W.FinishTime-W.StartTime). TotalseConds "Second"; this.btn_startwork.enabled = true "This.div_load.visible = false; break;} case 3: {this.Lab_jg.text =" The task ends, " (w.errorTime-w.starttime)). Totalseconds " Error leads to mission failure '"; this.btn_startwork.enabled = true; this.div_load.visible = false; Break;}} #Region Web Form Designer Generated Code Override Protected Void OnInit (Eventargs E) {// // Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} /// <summary> /// Designer Supports the required method - Do not use the code editor to modify the // / 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);</p> <p>} #Endregion</p> <p>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> ");}}}</p> <p>Public class work {public int state = 0; // 0- No start, 1- is running, 2- successful end, 3- Failure end public int init percent = 0; // Complete percentage PUBLIC dateTime StartTime; Public DateTime FinishTime; DateTime ErrorTime;</p> <p>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 ();}}}</p> <p>private void dowork () {try {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "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; // Is the percentage of definitions, how much time definition you estimate this operating fee} Conn.close (); // The above code performs a comparative time-consuming database operation State = 2;} catch {errortime = DateTime.now; percent = 0; State = 3;} finally {finishtime = datetime.now; percent = 0;}}}}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-37621.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="37621" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.070</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '1jEm9x_2FV2DdMbwgB0wcrK79RJxbBkzyDm5yHk717It9t7o9B3AJFdJt_2FDzdGbbBLShfCLeUKzYNeP0GirNKFrg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>