艺考文化课冲刺班:C#多线程编程:线程的启动

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 06:20:28
C#多线程编程:线程的启动来源:中国自学编程网   发布日期:2009-08-03  在实例化Thread的实例,需要提供一个委托,在实例化这个委托时所用到的参数是线程将来启动时要运行的方法。在.net中提供了两种启动线程的方式,一种是不带参数的启动方式,另一种是带参数的启动的方式。 
不带参数的启动方式 
如果启动参数时无需其它额外的信息,可以使用ThreadStart来实例化Thread,如下面的代码: 

view plaincopy to clipboardprint? 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Threading; 

namespace StartThread 

class Program 

int interval = 200; 
static void Main(string[] args) 

Program p = new Program(); 
Thread nonParameterThread = new Thread(new ThreadStart(p.NonParameterRun)); 
nonParameterThread.Start(); 

///  
/// 不带参数的启动方法 
///
 
public void NonParameterRun() 

for (int i = 0; i < 10; i++) 

Console.WriteLine("系统当前时间毫秒值:"+DateTime.Now.Millisecond.ToString()); 
Thread.Sleep(interval);//让线程暂停 



using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Threading; 

namespace StartThread 

class Program 

int interval = 200; 
static void Main(string[] args) 

Program p = new Program(); 
Thread nonParameterThread = new Thread(new ThreadStart(p.NonParameterRun)); 
nonParameterThread.Start(); 

///  
/// 不带参数的启动方法 
///
 
public void NonParameterRun() 

for (int i = 0; i < 10; i++) 

Console.WriteLine("系统当前时间毫秒值:"+DateTime.Now.Millisecond.ToString()); 
Thread.Sleep(interval);//让线程暂停 




程序的运行效果我们不用运行也会知道,那就是在循环中将系统当前时间的毫秒部分输出出来,在每次输出之后会将当前线程暂停一下,直到10次之后运行完毕,终止线程的执行。 
在上面的代码中我们是通过定义全局变量的方法来指定线程暂停间隔,按照这种方法,假如要运行10个线程,每个线程的暂停间隔不一样的话,就需要定义10个全局变量,虽然最终不影响系统的运行效果,但是总觉得不是太爽。 
有没有比较简单一点的办法呢?有!那就是使用带参数的启动方法。 
带参数的启动方法 
如果要在实例化线程时要带一些参数,就不能用ThreadStart委托作为构造函数的参数来实例化Thread了,而要ParameterizedThreadStart委托,和ThreadStart一样的是它也是线程启动时要执行的方法,和ThreadStart不同的是,它在实例化时可以用一个带有一个Object参数的方法作为构造函数的参数,而实例化ThreadStart时所用到的方法是没有参数的。 
为什么是Object这样的参数呢?很简单,因为在.net中Object是所有类型的基类,用它可以表示Array(数组)、Interface(接口)、ValueType(值类型,如bool,byte,char,short,int,float,long,double等)、class(类)等.net中的类型。当然,这也意味着如果你要启动一个线程,给它传递一个int类型参数时,必须在启动方法中进行相应的类型转换。 
下面就是一个例子,在启动线程时指定了线程的暂停间隔,代码如下: 

view plaincopy to clipboardprint? 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Threading; 

namespace StartThread 

class Program 

int interval = 200; 
static void Main(string[] args) 

Program p = new Program(); 

Thread parameterThread = new Thread(new ParameterizedThreadStart(p.ParameterRun)); 
parameterThread.Name = "Thread A:"; 
parameterThread.Start(30); 


///  
/// 带参数的启动方法 
///
 
/// 让线程在运行过程中的休眠间隔 
public void ParameterRun(object ms) 

int j = 10; 
int.TryParse(ms.ToString(), out j);//这里采用了TryParse方法,避免不能转换时出现异常 
for (int i = 0; i < 10; i++) 

Console.WriteLine(Thread.CurrentThread.Name+"系统当前时间毫秒值:" + DateTime.Now.Millisecond.ToString()); 
Thread.Sleep(j);//让线程暂停 




using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Threading; 

namespace StartThread 

class Program 

int interval = 200; 
static void Main(string[] args) 

Program p = new Program(); 

Thread parameterThread = new Thread(new ParameterizedThreadStart(p.ParameterRun)); 
parameterThread.Name = "Thread A:"; 
parameterThread.Start(30); 


///  
/// 带参数的启动方法 
///
 
/// 让线程在运行过程中的休眠间隔 
public void ParameterRun(object ms) 

int j = 10; 
int.TryParse(ms.ToString(), out j);//这里采用了TryParse方法,避免不能转换时出现异常 
for (int i = 0; i < 10; i++) 

Console.WriteLine(Thread.CurrentThread.Name+"系统当前时间毫秒值:" + DateTime.Now.Millisecond.ToString()); 
Thread.Sleep(j);//让线程暂停