using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//需要引入命名空间;
using System.Threading;
namespace ThreadCreate
{
/// <summary>
/// /创建一个Message测试类,利用Thread类循环输出10次一条语句;
/// </summary>
public class Message
{
public void ShowMessage() {
String message = string.Format("线程id是:{0}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine(message);
for (int n = 0; n < 10; n++) {
//休眠300毫秒
Thread.Sleep(300);
Console.WriteLine("当前时间是" + n.ToString());
}
}
}
}
主类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadCreate
{
class Program {
static void Main(string[] args)
{
//
Console.WriteLine("主线程id数为" + Thread.CurrentThread.ManagedThreadId);
Message message = new Message();
//Thread的
//Thread thread = new Thread(new ThreadStart(message.ShowMessage));
Thread thread = new Thread((message.ShowMessage));
thread.Start();//启动线程
Console.WriteLine("我是主线程正在做工作哦!");
Console.WriteLine("主线程工作终于做完了");
Console.ReadLine();
}
}
}