using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace MultiThread
{
class Program
{
int interval = 200;
static void Main(string[] args)
{
#region //测试不带参数的启动方法
//Program p = new Program();
//Thread nonParameterThread = new Thread(
// new ThreadStart(p.NonParameterRun));
//nonParameterThread.Start();
#endregion
#region //测试带参数的启动方法
//Program p = new Program();
//Thread parameterThread = new Thread(
// new ParameterizedThreadStart(p.ParameterRun));
//parameterThread.Name = "ThreadA:";
////这个值为30的int类型变量被装箱成object
//parameterThread.Start(30);
#endregion
#region //测试启动两个线程,每个线程的暂停间隔不一样
Program p = new Program();
//启动第一个线程
Thread parameterThread = new Thread(
new ParameterizedThreadStart(p.ParameterRun));
parameterThread.Name = "ThreadA:";
parameterThread.Start(30);
//启动第二个线程
parameterThread = new Thread(
new ParameterizedThreadStart(p.ParameterRun));
parameterThread.Name = "ThreadB:";
parameterThread.Start(60);
//输出为:
//说明线程是交叉运行的
//ThreadA:系统当前时间毫秒值:15
//ThreadB:系统当前时间毫秒值:15
//ThreadA:系统当前时间毫秒值:93
//ThreadB:系统当前时间毫秒值:125
//ThreadA:系统当前时间毫秒值:125
//ThreadA:系统当前时间毫秒值:156
//ThreadB:系统当前时间毫秒值:187
//ThreadA:系统当前时间毫秒值:187
//ThreadA:系统当前时间毫秒值:218
//ThreadB:系统当前时间毫秒值:250
//ThreadA:系统当前时间毫秒值:250
//ThreadA:系统当前时间毫秒值:281
//ThreadB:系统当前时间毫秒值:312
//ThreadA:系统当前时间毫秒值:312
//ThreadA:系统当前时间毫秒值:343
//ThreadB:系统当前时间毫秒值:375
//ThreadB:系统当前时间毫秒值:437
//ThreadB:系统当前时间毫秒值:500
//ThreadB:系统当前时间毫秒值:562
//ThreadB:系统当前时间毫秒值:625
#endregion
Console.ReadLine();
}
///
///不带参数的启动方法
///
public void NonParameterRun()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("系统当前时间毫秒值:" +
DateTime.Now.Millisecond.ToString());
Thread.Sleep(interval);//让线程暂停
}
}
///
///带参数的启动方法
///
///让线程在运行过程中的休眠间隔
public void ParameterRun(object ms)
{
int j=10;
//这里采用了TryParse方法,避免不能转换时出现异常
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);//让线程暂停
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadTest
{
//继续探索
//上面解决了一个问题,如果在启动线程时需要参数如何解决,如果针对上面的问题继续发掘
//,比如:在启动线程时不但要指定线程的暂停间隔,还需要指定循环次数(在上面的所有例
//子中都是执行10次的),这个问题该如何解决呢?
//有两种办法可以解决:
//首先可以继续在ParameterizedThreadStart这里做文章,因为这里可以使用一个Object类
//型的参数,那么可以通过数组或者一个类来解决(因为它们都是Object的子类)。我在做某
//个系统时确实采用数组处理过这种情况,这样就要求在线程启动方法中必须清楚知道数组
//中每个参数的用途,不是太方便。
//这里说说重新定义一个实体类来解决的方法,代码如下。
#region//第一种方法
/// <summary>
/// class MyThreadParameter
/// 只读属性:循环次数 LoopCount;线程的暂停间隔 Interval
/// </summary>
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="ThreadA:";
MyThreadParameter paramter=new MyThreadParameter(50,20);
parameterThread.Start(paramter);
Console.ReadLine();
}
///
/// ///带多个参数的启动方法
///
///方法参数
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);//让线程暂停
}
}
}
}
#endregion
//第二种方法和上面方法有些相似,也是需要引入外部类,并且将Thread实例放在引入的类
//中,这种情况适合于在线程中处理的业务逻辑比较复杂的情况。在前不久处理的一个项目
//中我用过这种情况,它是用来实现双向数据传输的。
#region//第二种方法
//class Program
//{
// /// <summary>
// /// 用来封装一个线程
// /// 字段:循环次数 LoopCount;线程的暂停间隔 Interval;线程实例 Thread
// /// </summary>
// class MyThreadParameter
// {
// private int interval;
// private int loopCount;
// private Thread thread;
// ///
// ///构造函数
// ///
// ///线程的暂停间隔
// ///循环次数
// public MyThreadParameter(int interval, int loopCount)
// {
// this.interval = interval;
// this.loopCount = loopCount;
// thread = new Thread(new ThreadStart(Run));
// }
// public void Start()
// {
// if (thread != null)
// {
// thread.Start();
// }
// }
// private void Run()
// {
// for (int i = 0; i < loopCount; i++)
// {
// Console.WriteLine("系统当前时间毫秒值:" +
// DateTime.Now.Millisecond.ToString());
// Thread.Sleep(interval);//让线程暂停
// }
// }
// }
// static void Main(string[] args)
// {
// MyThreadParameter parameterThread = new MyThreadParameter(30, 50);
// parameterThread.Start();
// Console.ReadLine();
// }
//}
#endregion
//上面的代码的运行效果和前面的代码运行效果类似,只不过是将业务处理代码放在一个单
//独的类MyThreadParameter中,使得MyThreadParameter看起来也像一个Thread,实际上维
//护的还是其内部的Thread,在一些大型系统中这样做的好处是便于维护。
//总结:在本篇主要讲述如何启动线程的问题,在启动时可能会遇到无需参数、需要多个参
//数的情况,在这里讲述了如何解决这些问题的思路。在.net类库中虽然存在着庞大的类库
//,但是并不是总会有合适的类来解决我们所遇到的问题,但是只要肯动脑筋总会想到合适
//的办法。
//本文出自 “周公的专栏” 博客 http://zhoufoxcn.blog.51cto.com/792419/187031
}