C#(asp.net)多線程用法示例(可用于同時處理多個任務(wù))
本文實(shí)例講述了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);
}
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#程序設(shè)計之線程使用技巧總結(jié)》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)繪制浮雕圖片效果實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)繪制浮雕圖片效果實(shí)例,是C#程序設(shè)計中非常實(shí)用的一個功能,需要的朋友可以參考下2014-08-08
c# 使用Task實(shí)現(xiàn)非阻塞式的I/O操作
這篇文章主要介紹了c# 使用Task實(shí)現(xiàn)非阻塞式的I/O操作,幫助大家更好的理解和學(xué)習(xí)c# 編程語言,感興趣的朋友可以了解下2020-11-11
C#實(shí)現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號的方法
這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)字節(jié)數(shù)截取字符串并加上省略號的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-07-07
C#開發(fā)Windows服務(wù)實(shí)例之實(shí)現(xiàn)禁止QQ運(yùn)行
這篇文章主要介紹了通過C#開發(fā)Windows服務(wù),查殺qq進(jìn)程的服務(wù)功能,需要的朋友可以參考下2013-10-10

