C# 多線程


簡易線程控制方法:

新建線程,無返回值,不帶參

Thread thread = new Thread(new ThreadStart());

thread.Start();

新建線程 ,無返回值,帶參數(shù)

Thread thread = new Thread(new ParameterizedThreadStart(object));

thread.Start(object);

主線程等待異步線程運行結束后繼續(xù)執(zhí)行

thread.join();

中斷當前線程,拋出ThreadAbortException 異常,異常參數(shù)ExceptionState為object

Thread.CurrentThread.Abort(object);

?//取消終止,繼續(xù)執(zhí)行線程
Thread.ResetAbort();


通過線程池控制:

工作者線程,直接使用方式,無返回值,需傳遞object對象,可不帶參數(shù)默認為null

缺點:不可帶多個參數(shù),不可返回值

?ThreadPool.QueueUserWorkItem(WaitCallback)
?ThreadPool.QueueUserWorkItem(WaitCallback,Object)?

使用委托實現(xiàn)工作者線程:

可傳參數(shù),可獲得返回值,可跟蹤線程狀態(tài),線程結束后可回調(diào)指定方法,并可帶有自定義參數(shù)

class Program
??? {
??????? delegate void MyDelegate();

??????? static void Main(string[] args)
??????? {
??????????? MyDelegate delegate1 = new MyDelegate(METHOD);
??????????? var methods=delegate1.GetType().GetMethods();
??????????? if (methods != null)
??????????????? foreach (MethodInfo info in methods)
??????????????????? Console.WriteLine(info.Name);
??????????? Console.ReadKey();
???????? }
???? }

委托實例重要方法

public class MyDelegate:MulticastDelegate
??? {
??????? public MyDelegate(object target, int methodPtr);
??????? //調(diào)用委托方法
??????? public virtual void Invoke();
??????? //異步委托
??????? public virtual IAsyncResult BeginInvoke(AsyncCallback callback,object state);
??????? public virtual void EndInvoke(IAsyncResult result);
??? }

當調(diào)用Invoke()方法時,對應此委托的所有方法都會被執(zhí)行。而BeginInvoke與EndInvoke則支持委托方法的異步調(diào)用,由BeginInvoke啟動的線程都屬于CLR線程池中的工作者線程,在下面將詳細說明。

IAsyncResult BeginInvoke(string name,AsyncCallback callback,object state)

異步調(diào)用委托方法,BeginInvoke 方法除最后的兩個參數(shù)外,其它參數(shù)都是與方法參數(shù)相對應的。通過 BeginInvoke 方法將返回一個實現(xiàn)了 System.IAsyncResult 接口的對象,之后就可以利用EndInvoke(IAsyncResult ) 方法就可以結束異步操作,獲取委托的運行結果。

BeginInvoke,不調(diào)用回調(diào)函數(shù)方法? ,代碼實例:

class Program
??? {
??????? delegate string MyDelegate(string name);

??????? static void Main(string[] args)
??????? {
??????????? ThreadMessage("Main Thread");
???????????
??????????? //建立委托
??????????? MyDelegate myDelegate = new MyDelegate(Hello);
??????????? //異步調(diào)用委托,獲取計算結果
??????????? IAsyncResult result=myDelegate.BeginInvoke("Leslie", null, null);
??????????? //在異步線程未完成前執(zhí)行其他工作
??????????? while (!result.IsCompleted)

??????????? {
??????????????? Thread.Sleep(200);????? //虛擬操作
??????????????? Console.WriteLine("Main thead do work!");
??????????? }

????????? //同等方法實現(xiàn)上述操作

?????????? while (!result.AsyncWaitHandle.WaitOne(200))
??????????? {
??????????????? Console.WriteLine("Main thead do work!");
??????????? }
??????????? string data=myDelegate.EndInvoke(result);
??????????? Console.WriteLine(data);
???????????
??????????? Console.ReadKey();
??????? }

??????? static string Hello(string name)
??????? {
??????????? ThreadMessage("Async Thread");
??????????? Thread.Sleep(2000);
??????????? return "Hello " + name;
??????? }

??????? static void ThreadMessage(string data)
??????? {
??????????? string message = string.Format("{0}\n? ThreadId is:{1}",
?????????????????? data,Thread.CurrentThread.ManagedThreadId);
??????????? Console.WriteLine(message);
??????? }
??? }

當要監(jiān)視多個運行對象的時候,使用IAsyncResult.WaitHandle.WaitOne可就派不上用場了。
幸好.NET為WaitHandle準備了另外兩個靜態(tài)方法:WaitAny(waitHandle[], int)與WaitAll (waitHandle[] , int)。
其中WaitAll在等待所有waitHandle完成后再返回一個bool值。
而WaitAny是等待其中一個waitHandle完成后就返回一個int,這個int是代表已完成waitHandle在waitHandle[]中的數(shù)組索引。
下面就是使用WaitAll的例子,運行結果與使用 IAsyncResult.IsCompleted 相同。

??????????? //此處可加入多個檢測對象
??????????? WaitHandle[] waitHandleList = new WaitHandle[] { result.AsyncWaitHandle,........ };
??????????? while (!WaitHandle.WaitAll(waitHandleList,200))
??????????? {
??????????????? Console.WriteLine("Main thead do work!");
??????????? }

BeginInvoke,調(diào)用回調(diào)函數(shù)方法? ,代碼實例:

委托結果包含重要方法

public interface IAsyncResult
{
??? object AsyncState {get;}??????????? //獲取用戶定義的對象,它限定或包含關于異步操作的信息。
??? WailHandle AsyncWaitHandle {get;}?? //獲取用于等待異步操作完成的 WaitHandle。
??? bool CompletedSynchronously {get;}? //獲取異步操作是否同步完成的指示。
??? bool IsCompleted {get;}???????????? //獲取異步操作是否已完成的指示。
}

如果想為回調(diào)函數(shù)傳送一些外部信息,就可以利用BeginInvoke(AsyncCallback,object)的最后一個參數(shù)object,它允許外部向回調(diào)函數(shù)輸入任何類型的參數(shù)。只需要在回調(diào)函數(shù)中利用 AsyncResult.AsyncState 就可以獲取object對象。

class Program
??? {
??????? public class Person
??????? {
??????????? public string Name;
??????????? public int Age;
??????? }

??????? delegate string MyDelegate(string name);

??????? static void Main(string[] args)
??????? {
??????????? ThreadMessage("Main Thread");

??????????? //建立委托
??????????? MyDelegate myDelegate = new MyDelegate(Hello);
???????????
??????????? //建立Person對象
??????????? Person person = new Person();
??????????? person.Name = "Elva";
??????????? person.Age = 27;
???????????
??????????? //異步調(diào)用委托,輸入?yún)?shù)對象person, 獲取計算結果
??????????? myDelegate.BeginInvoke("Leslie", new AsyncCallback(Completed), person);???????????
?????????
??????????? //在啟動異步線程后,主線程可以繼續(xù)工作而不需要等待
??????????? for (int n = 0; n < 6; n++)
??????????????? Console.WriteLine("? Main thread do work!");
??????????? Console.WriteLine("");

??????????? Console.ReadKey();
??????? }

??????? static string Hello(string name)
??????? {
??????????? ThreadMessage("Async Thread");
??????????? Thread.Sleep(2000);
??????????? return "\nHello " + name;
??????? }

??????? static void Completed(IAsyncResult result)
??????? {
??????????? ThreadMessage("Async Completed");

??????????? //獲取委托對象,調(diào)用EndInvoke方法獲取運行結果
??????????? AsyncResult _result = (AsyncResult)result;
??????????? MyDelegate myDelegate = (MyDelegate)_result.AsyncDelegate;
??????????? string data = myDelegate.EndInvoke(_result);
??????????? //獲取Person對象
??????????? Person person = (Person)result.AsyncState;
??????????? string message = person.Name + "'s age is " + person.Age.ToString();

??????????? Console.WriteLine(data+"\n"+message);
??????? }

??????? static void ThreadMessage(string data)
??????? {
??????????? string message = string.Format("{0}\n? ThreadId is:{1}",
?????????????????? data, Thread.CurrentThread.ManagedThreadId);
??????????? Console.WriteLine(message);
??????? }
??? }


號外引申:

事件注冊委托,實現(xiàn)廣播功能

A首先聲明 委托和事件

??????? public delegate void DisplayUpdate(string message);
??????? //聲明事件
??????? public event DisplayUpdate ShowUpdate;

B?定義A的實例并 注冊事件

loginForm.ShowUpdate += new LoginForm.DisplayUpdate(loginincallback);

A調(diào)用事件,實現(xiàn)廣播功能

??????????????? if (ShowUpdate != null) ShowUpdate(message);

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,973評論 19 139
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內(nèi)部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 1、線程分為前臺線程和后臺線程,應用程序在所有前臺線程結束后退出。 2、使用Thread創(chuàng)建前臺線程: 3、使用T...
    金戈大王閱讀 4,911評論 3 5
  • 一個類要么是多線程安全的,要么它就不是。 前言 多線程情況下的隨機crash是在iOS 編程過程中比較常見的一種?...
    可樂偶滴最耐閱讀 2,173評論 0 1
  • 7/100 周衛(wèi) 河南 【每日一結構】 序言:從早上就開始想今天拆什么內(nèi)容的結構思考力,想了一天也沒有頭緒,頭腦中...
    心理實踐者周周閱讀 123評論 0 0