class Program
{
static void Main(string[] args)
{
Console.WriteLine("Begin in Main");
Thread thread = new Thread(new ThreadStart(ThreadInvoke));
//启动线程 这是最简单的例子
thread.Start();
//将当前线程挂起200毫秒
Thread.Sleep(200);
Console.WriteLine("End in Main");
}
static void ThreadInvoke()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Execute in ThreadInvoke");
//每隔100毫秒,循环一次
Thread.Sleep(100);
}
}
}
class Program
{
static void Main(string[] args)
{
//这是使用线程池的例子
Console.WriteLine("Begin in Main");
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadInvoke));
//将当前线程挂起200毫秒
Thread.Sleep(200);
Console.WriteLine("End in Main");
Thread.Sleep(3000);
}
static void ThreadInvoke(Object param)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Execute in ThreadInvoke");
//每隔100毫秒,循环一次
Thread.Sleep(100);
}
}
}