C#(asp.net)多線程用法示例(可用於同時處置多個義務)。本站提示廣大學習愛好者:(C#(asp.net)多線程用法示例(可用於同時處置多個義務))文章只能為提供參考,不一定能成為您想要的結果。以下是C#(asp.net)多線程用法示例(可用於同時處置多個義務)正文
本文實例講述了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#法式設計有所贊助。