当前位置:K88软件开发文章中心编程语言.NETASP.NET → 文章内容

ASP.NET 多线程

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:58:17

<asp:Label ID="lblmessage" runat="server" Text="Label"> </asp:Label> </form> </body> </html>后台代码如下: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Threading; namespace threaddemo { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ThreadStart childthreat = new ThreadStart(childthreadcall); Response.Write("Child Thread Started <br/>"); Thread child = new Thread(childthreat); child.Start(); Response.Write("Main sleeping for 2 seconds.......<br/>"); Thread.Sleep(2000); Response.Write("<br/>Main aborting child thread<br/>"); child.Abort(); } public void childthreadcall() { try{ lblmessage.Text = "<br />Child thread started <br/>"; lblmessage.Text += "Child Thread: Coiunting to 10"; for( int i =0; i<10; i++) { Thread.Sleep(500); lblmessage.Text += "<br/> in Child thread </br>"; } lblmessage.Text += "<br/> child thread finished"; }catch(ThreadAbortException e){ lblmessage.Text += "<br /> child thread - exception"; }finally{ lblmessage.Text += "<br /> child thread - unable to catch the exception"; } } } }观察以下:当页面被加载时,一个新的线程会以 childthreadcall() 为参考开始启动。主线程的活动会直接显示在网页。 第二个线程运行并将消息发送到控制标签。 在子线程执行时主线程休眠 2000 毫秒。子线程持续运行直到它被主线程中止,然后它会抛出 ThreadAbortException 异常并被终止。 控制返回到主线程。 当执行时程序会发送以下信息:

上一页  [1] [2] 


ASP.NET 多线程