本文实例讲述了C#(asp.net)多线程用法。分享给大家供大家参考,具体如下:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Threading;using System.Web.UI.WebControls;public partial class muti_thread : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { Thread Thread1 = new Thread(new ThreadStart(CalcSum)); Thread Thread2 = new Thread(new ThreadStart(CalcGap)); Thread1.Start(); Thread2.Start(); Thread1.Join(); Thread2.Join(); } //求和方法 protected void CalcSum() { long sum = 0; for (long i = 0; i < 100; i++) { sum += i; Response.Write(string.Format("Thread1-->i={0}:sum={1}<br/>", i, sum)); Response.Flush(); System.Threading.Thread.Sleep(5000); } } //求差方法 protected void CalcGap() { long gap = 0; for (long i = 100; i >= 0; i--) { gap = i - 1; Response.Write(string.Format("Thread2-->i={0}:gap={1}<br/>", i, gap)); Response.Flush(); System.Threading.Thread.Sleep(1000); } }}
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#程序设计之线程使用技巧总结》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。
新闻热点
疑难解答