码迷,mamicode.com
首页 > 编程语言 > 详细

C#之线程ThreadStart

时间:2019-07-06 22:09:06      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:方法   tar   read   需要   创建线程   返回值   nbsp   运行   lin   

本来自于https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.threadstart?view=netframework-4.8;

static void Main(string[] args)
        {
            ThreadStart threadStartDelegate = new ThreadStart(Work.DoWork);   //创建委托
            Thread thread = new Thread(threadStartDelegate);  //用ThreadStart委托实例化线程Thread
            thread.Start();
            Work work = new Work();
            work.Data = 42;
            threadStartDelegate = new ThreadStart(work.DoMoreWork);
            thread = new Thread(threadStartDelegate);
            thread.Start();
            Console.ReadKey();
        }
 public class Work
    {
        public static void DoWork()
        {
            Console.WriteLine("Static thread procedure.");
        }
        public int Data;
        public void DoMoreWork()
        {
            Console.WriteLine("Instance thread procedure. Data={0}", Data);
        }
    }
       C#使用线程时首先需要创建线程,使用Thread类构造函数创建实例需要用到ThreadStart委托或者ParameterizedThreadStart 委托创建 Thread 类的实例,ThreadStart 委托只能用于无返回值、无参数的方法,ParameterizedThreadStart 委托则可以用于带参数的方法。线程不会直接运行,直至调用Start()方法时为止。

C#之线程ThreadStart

标签:方法   tar   read   需要   创建线程   返回值   nbsp   运行   lin   

原文地址:https://www.cnblogs.com/xingyuanzier/p/11144322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!