在实例化Thread的实例,需要提供一个委托,在实例化这个委托时所用到的参数是线程将来启动时要运行的方法。在.net中提供了两种启动线程的方式,一种是不带参数的启动方式,另一种是带参数的启动的方式。
不带参数的启动方式
- 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();
- }
- /// <summary>
- /// 不带参数的启动方法
- /// </summary>
- public void NonParameterRun()
- {
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine("系统当前时间毫秒值:"+DateTime.Now.Millisecond.ToString());
- Thread.Sleep(interval);//让线程暂停
- }
- }
- }
带参数的启动方法
如果要在实例化线程时要带一些参数,就不能用ThreadStart委托作为构造函数的参数来实例化Thread了,而要ParameterizedThreadStart委托,和ThreadStart一样的是它也是线程启动时要执行的方法,和ThreadStart不同的是,它在实例化时可以用一个带有一个Object参数的方法作为构造函数的参数,而实例化ThreadStart时所用到的方法是没有参数的。
为什么是Object这样的参数呢?很简单,因为在.net中Object是所有类型的基类,用它可以表示Array(数组)、Interface(接口)、ValueType(值类型,如bool,byte,char,short,int,float,long,double等)、class(类)等.net中的类型。当然,这也意味着如果你要启动一个线程,给它传递一个int类型参数时,必须在启动方法中进行相应的类型转换。
- 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);
- }
- /// <summary>
- /// 带参数的启动方法
- /// </summary>
- /// <param name="ms">让线程在运行过程中的休眠间隔</param>
- 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);//让线程暂停
- }
- }
- }
- }
给多线程传参的三种方式
上面是一种
(2)创建自定义类
using System;
using System.Threading;
namespace ThreadWithParameters
{
public class MyThread
{
private string data;
public MyThread(string data)
{
this.data = data;
}
public void ThreadMain()
{
Console.WriteLine("Running in a thread,data: {0}", data);
}
}
class Program
{
static void Main(string[] args)
{
MyThread myThread = new MyThread("hello world");
Thread thread = new Thread(myThread.ThreadMain);
thread.Start();
Console.Read();
}
}
}
(3)使用匿名方法
class Program
{
static void Main(string[] args)
{
string hello = "hello world";
//如果写成Thread thread = new Thread(ThreadMainWithParameters(hello));这种形式,编译时就会报错
Thread thread = new Thread(() => ThreadMainWithParameters(hello));
thread.Start();
Console.Read();
}
static void ThreadMainWithParameters(string str)
{
Console.WriteLine("Running in a thread,received: {0}", str);
}
如果在启动线程时需要参数,而且在启动线程时不但要指定线程的暂停间隔,还需要指定循环次数(即多参数)
首先可以继续在ParameterizedThreadStart这里做文章,因为这里可以使用一个Object类型的参数,那么可以通过一个类来解决
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- namespace StartThread
- {
- class MyThreadParameter
- {
- private int interval;
- private int loopCount;
- /// <summary>
- /// 循环次数
- /// </summary>
- public int LoopCount
- {
- get { return loopCount; }
- }
- /// <summary>
- /// 线程的暂停间隔
- /// </summary>
- public int Interval
- {
- get { return interval; }
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="interval">线程的暂停间隔</param>
- /// <param name="loopCount">循环次数</param>
- public MyThreadParameter(int interval,int loopCount)
- {
- this.interval = interval;
- this.loopCount = loopCount;
- }
- }
- class Program
- {
- int interval = 200;
- static void Main(string[] args)
- {
- Program p = new Program();
- Thread parameterThread = new Thread(new ParameterizedThreadStart(p.MyParameterRun));
- parameterThread.Name = "Thread A:";
- MyThreadParameter paramter = new MyThreadParameter(50, 20);
- parameterThread.Start(paramter);
- }
- /// <summary>
- /// 带多个参数的启动方法
- /// </summary>
- /// <param name="ms">方法参数</param>
- public void MyParameterRun(object ms)
- {
- MyThreadParameter parameter = ms as MyThreadParameter;//类型转换
- if (parameter != null)
- {
- for (int i = 0; i < parameter.LoopCount; i++)
- {
- Console.WriteLine(Thread.CurrentThread.Name + "系统当前时间毫秒值:" + DateTime.Now.Millisecond.ToString());
- Thread.Sleep(parameter.Interval);//让线程暂停
- }
- }
- }
- }
- }