C#之線程ThreadStart


本來自於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()方法時為止。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM