C#编程-147:线程基础

启动线程:

ThreadStart ts = new ThreadStart(method);//创建委托实例
Thread t = new Thread(ts);//创建线程
t.Start();//启动线程


线程休眠:

t.Suspend();//线程挂起
Thread.Sleep(1000);//线程休眠
t.Resume();//线程继续

属性:IsAlive,判断线程当前执行状态

C#编程-147:线程基础C#编程-147:线程基础C#编程-147:线程基础C#编程-147:线程基础

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadPriorityTest
{
    class Program
    {
        public static void method()
        {
            Console.WriteLine("线程名称:{0},线程优先级:{1}",
                            Thread.CurrentThread.Name.ToString(),
                            Thread.CurrentThread.Priority.ToString());
 
        }
        static void Main(string[] args)
        {
            ThreadStart ts1 = new ThreadStart(method);//创建委托实例
            ThreadStart ts2 = new ThreadStart(method);
            Thread t1 = new Thread(ts1);//创建线程
            Thread t2 = new Thread(ts2);
            t1.Name = "线程1";
            t2.Name = "线程2";
            t1.Priority = ThreadPriority.Highest;
            t2.Priority = ThreadPriority.BelowNormal;
            t1.Start();
            t2.Start();
            Console.ReadKey();
 
        }
    }
}
上一篇:多线程同步基础


下一篇:Crystal Reports拉报表报错:Error detected by database DLL