C#线程(基本用法)

版权声明:博主原创/资料整理,转载请注明出处!! https://blog.csdn.net/tiegenZ/article/details/80825196

线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元。

通过System.Threading.Thread类可以开始新的线程,并在线程堆栈中运行静态或实例方法。

示例:

1.运行静态方法

static void Main()
{
 Thread t = new Thread(test);
 t.Start();
 Console.Write("start");
 Console.Write("\r\n");
 Console.Read();
}
public static void test()
{
   Console.Write("hello Thread");
   Console.Read();

}

输出效果图:


2.运行实例方法

static void Main()

{
  Thread t1 = new Thread(new Program().Test1);
  t1.Start();
  Console.Write("start");
  Console.Write("\r\n");
  Console.Read();
}
public void Test1()
{
  Console.Write("hello Thread1");
  Console.Read();
}

输出效果图:




猜你喜欢

转载自blog.csdn.net/tiegenZ/article/details/80825196