美文网首页
C#之Thread开启线程

C#之Thread开启线程

作者: 菜鸟程序猿 | 来源:发表于2017-08-11 12:09 被阅读0次

首先需要引用Thread这个类的命名空间using System.Threading;

namespace Thread开启线程
{
    class Program
    {
     
        static void Main(string[] args)
        {

            Thread t = new Thread(Test2);//参数指向无参无返回值的函数,或者有一个参数无返回值的函数
            t.Start("xxx");//开启线程
----------------------------------------------------------------------------------------------------------------
            若有多个参数,可以写成一个类的多个字段,重写有参构造函数对字段进行赋值,再写一个无参函数调用多个字段的方法
            MyThread mt = new MyThread(1, "张三");
            Thread t = new Thread(mt.Play);
            t.Start();
            Console.ReadKey();

        }
      
        static void Test(object obj)
        {
            Console.WriteLine("Test");
        }
    }

    class MyThread
    {
        private int Id;
        private string Name;

        public MyThread(int id,string name)
        {
            this.Id = id;
            this.Name = name;
        }
        public void Play()
        {
            Console.WriteLine(Id+Name+"Playing");
        }
    }
}

相关文章

网友评论

      本文标题:C#之Thread开启线程

      本文链接:https://www.haomeiwen.com/subject/sgelrxtx.html