C#添加线程

首次发布:2017-09-04 17:19
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(new ThreadStart(TestMethod));
            Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));
            t1.IsBackground = true;
            t2.IsBackground = true;
             if(!t1.IsAlive)//线程状态,如果线程已在运行则不运行
             {
               t1.Start();
              }
             if(!t2.IsAlive)
             {
            t2.Start("hello");
             }
            Console.ReadKey();
        }

        public static void TestMethod()
        {
            Console.WriteLine("不带参数的线程函数");
        }

        public static void TestMethod(object data)
        {
            string datastr = data as string;
            Console.WriteLine("带参数的线程函数,参数为:{0}", datastr);
        }
    } 
}