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(); } /// <summary> /// 不带参数的启动方法 /// </summary> 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(); } /// <summary> /// 不带参数的启动方法 /// </summary> public void NonParameterRun() { for (int i = 0; i < 10; i++) { Console.WriteLine("系统当前时间毫秒值:"+DateTime.Now.Millisecond.ToString()); Thread.Sleep(interval);//让线程暂停 } } }
/// <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);//让线程暂停 } } } } 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();