using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
class MyApp
{
    static int i = 0;
    public static void Main()
    {
        for (i = 0; i < 10; i++)
        {
            Thread thread = new Thread(new ThreadStart(ThreadFunc));
            thread.IsBackground = true;                    //标记为后台线程。
            thread.Start();
        }
    }
    private static void ThreadFunc()
    {
        int j = i;
        DateTime start = DateTime.Now;
        while ((DateTime.Now - start).Seconds < 2)
        {
            //每个线程执行2秒
        }
        Console.WriteLine("第 {0} 个线程结束",j);
        
           
    }
}
//前台线程和后台线程。这两者的区别就是:应用程序必须运行完所有的前台线程才可以退出;默认为前台线程。
//而对于后台线程,应用程序则可以不考虑其是否已经运行完毕而直接退出,所有的后台线程在应用程序退出时都会自动结束。 
//一个线程是前台线程还是后台线程可由它的IsBackground属性来决定.

评论