当前位置:编程学习 > C#/ASP.NET >>

大家提供给我一个asp.net多线程的例子

给我一个完成的多线程的例子,里面只要能实现一个功能就OK了,希望能有详细的注释
答案:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

namespace ThreadEag

{

    class Program

    {

        static void Main(string[] args)

        {

            //调用线程方法

            ThreadPro();

 

            //让控制台停窗体停下来,便于观察

            Console.ReadKey();

        }

 //主方法,包括开启线程方法

        public static  void ThreadPro()

        {

            Console.WriteLine("Main Thread:Start a second work !");

 

            //声明一个线程,并指明调用方法:ThreadWork

            Thread t = new Thread(new ThreadStart(ThreadWork));

 

            //开启一个线程

            t.Start();

           

            for (int i = 0; i < 4;i++ )

            {

                Console.WriteLine("Main Thread:Do some work!");

                //线程休眠时间

                Thread.Sleep(0);

            }

 

            Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");

            t.Join();

            Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");

        }

 

        //被线程调用方法

        public static void ThreadWork()

        {

            //要干的具体事,可以在这个方法里面实现

            Console.WriteLine("ThreadWork start work!");

           

            for (int i = 0; i < 10; i++)

            {

                Console.WriteLine("ThreadWork:{0}",i);

                Thread.Sleep(0);

            }

        }

    }

}

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

using System.Threading;

namespace ThreadEag

{

    class WorkPace

    {

        //泛型创建一个People集合,

        private static List<People> list;

        public static List<People> List

        {

            get { return list; }

            set { list = value; }

        }      

        public void ThreadWork()

        {

            //初始化三个people事例

            People a = new People();

            People b = new People();

            People c = new People();

 

            //分别给他们的状态赋值

            a.State = "A";

            b.State = "B";

            c.State = "C";

 

            //初始化集合类对象

            list = new List<People>();

 

            //将人员添加的集合里面

            list.Add(a);

            list.Add(b);

            list.Add(c);

 

            //初始化三个线程对象

            Thread ta = new Thread(new ThreadStart(a.Work));

            Thread tb = new Thread(new ThreadStart(b.Work));

            Thread tc = new Thread(new ThreadStart(c.Work));

            //开启三个线程

            ta.Start();

   tb.Start();

       

上一个:asp.net后台为页面控件注册事件的问题
下一个:asp.net中怎么取消一个项目的启动项目?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,