网络通信 频道

.NET下的多线程学习演示

 using System;
  using System.Threading;
  
  namespace ConsoleApplication1
  {
  ///
  /// Summary description for Class1.
  ///

  class Class1
  {
  // 记录已经完成的线程数
  static int Cnt = 0;
  ///
  /// The main entry point for the application.
  ///

  [STAThread]
  
  static void Main(string[] args)
  {
  //
  // TODO: Add code to start application here
  //
  int i;
  for (i = 0; i<5; i++)
  {
  // 创建一个新线程
  Thread thread = new Thread(new ThreadStart(printx));
  // 设置为后台线程,主线程结束里程序结束
  thread.IsBackground = true;
  // 设置线程的优先级
  //thread.Priority = ThreadPriority.AboveNormal;
  // 执行一个线程
  thread.Start();
  }
  }
  // 线程函数:(比起VC6中的方便多啦,哈哈)
  // 一个线程的方法不包含任何参数,同时也不返回任何值。它的命名规则和
  // 一般函数的命名规则相同。它既可以是静态的(static)也可以是非静态
  // 的(nonstatic)。当它执行完毕后,相应的线程也就结束了
  public static void printx()
  {
  Cnt += 1;
  Console.Write("Thread {0} end.\n",Cnt);
  }
  }
  }
  
文章转载地址:http://www.cnpaf.net/Class/hack/06101110491599261555.html
0
相关文章