.NET下的多线程学习演示

时间:2006-10-12 来源: 作者: 点击:
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 voi
  
  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);
  }
  }
  }
  
  
------分隔线----------------------------
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
推荐内容