C# 线程基础

1  线程是进程中的一个执行流 

2 线程是一个可以单独操作的活动

3 线程创建和常用方法

  a  创建   

   Thread  th=new Thread(Method);

 b 常见方法

  th.start() //启动线程

  th.Abort()//终止线程

  Thread.Sleep(n)//休眠线程 (停止n毫秒后继续执行)

  th.Suspened()//挂起线程,在调用resume之后,若线程已挂起则无作用

  th.Resume()//恢复线程//恢复挂掉的线程对正在运行的线程无作用

  th.Interrupt()//终止线程 用于终止 处于sleep 或者 wait  join 之后后的线程

 c 线程常见属性

d  线程状态类 ThreadState

经常用来存储线程状态

例如 Threadstate tmp = th.ThreadState;

ThreadState 是 Thread的一个属性 

e 线程 优先级  

ThreadPriority   ,通常值为

 // 摘要:
        //     可以将 System.Threading.Thread 安排在具有任何其他优先级的线程之后。
        Lowest = 0,
        //
        // 摘要:
        //     可以将 System.Threading.Thread 安排在具有 Normal 优先级的线程之后,在具有 Lowest 优先级的线程之前。
        BelowNormal = 1,
        //
        // 摘要:
        //     可以将 System.Threading.Thread 安排在具有 AboveNormal 优先级的线程之后,在具有 BelowNormal 优先级的线程之前。默认情况下,线程具有
        //     Normal 优先级。
        Normal = 2,
        //
        // 摘要:
        //     可以将 System.Threading.Thread 安排在具有 Highest 优先级的线程之后,在具有 Normal 优先级的线程之前。
        AboveNormal = 3,
        //
        // 摘要:
        //     可以将 System.Threading.Thread 安排在具有任何其他优先级的线程之前。
        Highest = 4,

 //设置线程优先级

Thread.Priority = ThreadPriority.X ;.//X为线程优先级

评论

暂无评论