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 }
  • 相关阅读:
    Linq to SQL -- 入门篇
    进程和线程(线程是轻量级进程)(上)
    复制文件夹中的所有文件夹与文件到另一个文件夹
    C# 特性(Attribute)
    文件的输入与输出
    正则表达式
    预处理指令
    String类
    可空类型(Nullable)
    参数传递
  • 原文地址:https://www.cnblogs.com/HighFun/p/2454613.html
Copyright © 2011-2022 走看看