zoukankan      html  css  js  c++  java
  • C#多线程学习笔记(一)

    Thread C#的线程类:

    Thrd = new Thread(this.Run); Run就是我们线程要执行的函数

    函数类型必须是这样,void Run();

    Thrd = new Thread(this.Run);
    Thrd.Name = name; //线程的名字
    Thrd.Start(); //就是开始线程

    Thread.Sleep(250);//休眠250ms

    Thrd.Suspend();//线程挂起

    Thrd.Resume();//恢复

    Thrd.Join(); //等待线程执行结束以后才继续执行下面的语句

     1 namespace Thread_CS
     2 {
     3     class MyThread
     4     {
     5         public Thread Thrd;
     6         public MyThread(string name)
     7         {
     8             Thrd = new Thread(this.Run);
     9             Thrd.Name = name;
    10             Thrd.Start();
    11         }
    12         // This is the entry point for thread.
    13         void Run()
    14         {
    15             try
    16             {
    17                 Console.WriteLine(Thrd.Name + " starting.");
    18                 for (int i = 1; i <= 100; i++)
    19                 {
    20                     Console.Write(i + " ");
    21                     if ((i % 10) == 0)
    22                     {
    23                         Console.WriteLine();
    24                         Thread.Sleep(250);
    25                     }
    26                 }
    27                 Console.WriteLine(Thrd.Name + " exiting normally.");
    28             }
    29             catch (ThreadAbortException exc)
    30             {
    31                 Console.WriteLine("Thread aborting, code is " +
    32                                    exc.ExceptionState);
    33 //                 Thread.ResetAbort();
    34             }
    35 //             Console.WriteLine(Thrd.Name + " exiting normally.");
    36         }
    37     }
    38 
    39     class Program
    40     {
    41         static void Main(string[] args)
    42         {
    43             MyThread mt1 = new MyThread("My Thread");
    44             Thread.Sleep(1000); // let child thread start executing
    45             
    46             mt1.Thrd.Suspend();
    47             Console.WriteLine(mt1.Thrd.ThreadState);
    48             mt1.Thrd.Resume();
    49             mt1.Thrd.Join(); // wait for thread to terminate
    50             Console.WriteLine("Main thread terminating.");
    51 
    52         }
    53     }
    54 }
  • 相关阅读:
    4月1日作业总结,成绩
    4月8日作业重点及点评
    构建之法 东北师大站 冲刺领跑
    4月1日作业重点及点评
    3月25日作业总结,成绩
    3月18日作业总结,成绩
    3月25日作业重点及点评
    python: 反射机制;
    python: 基本知识(一)
    opencv: 轮廓提取;
  • 原文地址:https://www.cnblogs.com/HighFun/p/2454613.html
Copyright © 2011-2022 走看看