码迷,mamicode.com
首页 > 编程语言 > 详细

c#(asp.net) 多线程示例,用于同时处理多个任务

时间:2015-03-10 17:16:02      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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#(asp.net) 多线程示例,用于同时处理多个任务

标签:

原文地址:http://blog.csdn.net/smartsmile2012/article/details/44177747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!