.NET下的多线程学习演示

王朝c#·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

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

{

// 创建一个新线程

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);

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航