在实例化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();
- }
-
-
-
- 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);
- }
-
-
-
-
-
- public void ParameterRun(object ms)
- {
- int j = 10;
- int.TryParse(ms.ToString(), out j);
- 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";
如果在启动线程时需要参数,而且在启动线程时不但要指定线程的暂停间隔,还需要指定循环次数(即多参数)
首先可以继续在ParameterizedThreadStart这里做文章,因为这里可以使用一个Object类型的参数,那么可以通过一个类来解决
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
-
- namespace StartThread
- {
- class MyThreadParameter
- {
- private int interval;
- private int loopCount;
-
-
-
- public int LoopCount
- {
- get { return loopCount; }
- }
-
-
-
-
- public int Interval
- {
- get { return interval; }
- }
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- 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);
- }
- }
- }
- }
- }
|
请发表评论