1. 程式人生 > 實用技巧 >多執行緒---Thread理解(二)

多執行緒---Thread理解(二)

一、Thread類:NetFramework 1.0 1.1 Thread是最初版本,功能非常強大,唯一的前臺執行緒。在System.Threading 名稱空間下,包含了建立和操作執行緒。

二、啟用執行緒

//ThreadStart 沒有引數的委託
ThreadStart threadStart = () => Todo("ThreadMethod");

//Thread 接收一個 ThreadStart 物件的引數
Thread thread = new Thread(threadStart);

//開啟執行緒,執行委託
thread.Start();

//ParameterizedThreadStart 帶引數的委託
ParameterizedThreadStart parameterizedThreadStart = s => Todo("ThreadMethod"); //Thread 接收一個 ThreadStart 物件的引數 Thread thread = new Thread(parameterizedThreadStart); //開啟執行緒,執行委託 thread.Start("123");

三、暫停執行緒

thread.Suspend();

四、恢復暫停

thread.Resume();

五、掛起執行緒

thread.Abort();

六、等待執行緒執行完後再執行

//1.判斷執行緒是否已經停止,如果執行緒已經停止,繼續執行
while (thread.ThreadState!=ThreadState.Stopped) { Thread.Sleep(200); } //2. join方法等待 thread.Join();

七、優先順序 Highest:最高優先順序 預設為:Normal

thread.Priority = ThreadPriority.Highest;

八、設定執行緒為前臺執行緒/後臺執行緒

thread.IsBackground = false;  //前臺執行緒  程序關閉之後,執行緒必須執行完了再退出
thread.IsBackground = true;  //後臺執行緒  程序關閉之後,執行緒也同時退出

九、回撥,啟動子執行緒執行動作A,不阻塞,A執行完了之後,子執行緒在執行動作B

        private void ThreadCallBack(ThreadStart threadStart, Action action)
        {
            ThreadStart start = new ThreadStart(() =>
            {
                threadStart.Invoke();
                action.Invoke();
            });
            new Thread(start).Start();
        }
     //呼叫回撥方法 ThreadStart threadStart
= new ThreadStart(() => { Thread.Sleep(2000); Console.WriteLine("threadStart 開始執行"); }); Action callBack = () => { Console.WriteLine("子執行緒執行完了,在執行回撥...."); }; ThreadCallBack(threadStart, callBack);

十、封裝一個非同步方法,並且返回執行的結果

        private Func<T> ThreadReturn<T>(Func<T> func)
        {
            T t = default(T);
            ThreadStart threadStart = () =>
            {
                t = func.Invoke();
            };
            var thread = new Thread(threadStart);
            thread.Start();

            return new Func<T>(() =>
            {
                thread.Join();
                return t;
            });
        }
          //呼叫
                ThreadStart threadStart = new ThreadStart(() =>
                {
                    Thread.Sleep(2000);
                    Console.WriteLine("threadStart 開始執行");
                });
                Func<int> func = () =>
                {
                    Thread.Sleep(2000);
                    return DateTime.Now.Year;
                };

                var threadReturn = ThreadReturn(func);
                var result = threadReturn.Invoke();
                Console.WriteLine($"result={result}");
     private void Todo(string name)
        {
            Console.WriteLine($"Todo 方法開始執行....");

            long result = 0;
            for (int i = 0; i < 100000; i++)
            {
                result += i;
            }
            Thread.Sleep(500);
            Console.WriteLine($"Todo方法執行的結果:{result},名稱{name},ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}");

            Console.WriteLine($"Todo 方法結束執行....");
        }