C#线程示例三

来源:互联网 发布:网络系统验收标准 编辑:程序博客网 时间:2024/06/06 17:58

这是一个简单的主线程与子线程

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading;

namespace ZiZhuXiancheng

{

    class Program

    {

        static void Main(string[] args)

        {

            Thread.CurrentThread.Name = "主线程";  //给主线程一个名字

            Console.WriteLine("{0}启动",Thread.CurrentThread.Name);

            Console.WriteLine();

            //实例化一个有参委托

            ParameterizedThreadStart thrTerThr = new ParameterizedThreadStart(LeiJia);

            Thread thr = new Thread(thrTerThr);

            thr.Name = "子线程:";

            thr.Start(10);

            //thr.Suspend();  //挂起子线程

        }

        public static void LeiJia(object num)

        {

            int sum = 0;

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

            {

                sum += (int)num;

                Console.WriteLine("{0},sum = {1}",Thread.CurrentThread.Name,sum);

                Thread.Sleep(1000);

            }

        }

    }

}