精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数
生活随笔
收集整理的這篇文章主要介紹了
精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[索引頁]
[源碼下載]
精進不休 .NET 4.0 (4) - C# 4.0 新特性之命名參數和可選參數, 動態綁定(dynamic), 泛型協變和逆變, CountdownEvent, Barrier
作者:webabcd
介紹
C# 4.0 的新特性
示例
1、?命名參數和可選參數的 Demo
NamedAndOptionalArguments.aspx.cs /*?
* 命名參數和可選參數?
* 命名參數:調用方法時,可以不按位置傳遞參數,而是指定參數的命名來傳值?
* 可選參數:聲明方法中的參數時,可以為其設置默認值,那么在調用該方法時,這種可選參數是可以忽略的?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?partial?class?NamedAndOptionalArguments : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????Write("hello");?
????????????????????????Write("hello",?"webabcd");?
????????????????????????Write("hello", p3:?false, p2:?"webabcd");?
????????????????}?
void Write() void Write(string?p1,?string?p2 =?"p2", bool p3 =?true)?
????????????????{?
????????????????????????Response.Write(string.Format("p1:{0}; p2:{1}; p3:{2}", p1, p2, p3.ToString()));?
????????????????????????Response.Write("<br />");?
????????????????}?
????????}?
}?
/*?
運行結果:?
p1:hello; p2:p2; p3:True?
p1:hello; p2:webabcd; p3:True?
p1:hello; p2:webabcd; p3:False?
*/ 2、dynamic 的 Demo
DynamicBinding.aspx.cs /*?
* dynamic - 用于動態編程,其依賴于Dynamic Language Runtime(DLR)?
*????
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?class?DyanmicDemo?
????????{?
string?Hello()?string?Hello(string?name)?
????????????????{?
????????????????????????return?"hello: "?+ name;?
????????????????}?
????????????????public?string?Name {?get;?set; }?
????????????????public?string?this[string?value]?
????????????????{?
????????????????????????get?
????????????????????????{?
????????????????????????????????return value;?
????????????????????????}?
????????????????}?
dynamic GetNames() dynamic GetNames()?
????????????????{?
????????????????????????List<string> names =?new?List<string>() {?"web",?"webabc",?"webabcd"?};?
?????????????????????????
????????????????????????return names;?
????????????????}?
????????}?
????????public?partial?class?DynamicBinding : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????dynamic d =?new?DyanmicDemo();?
????????????????????????Response.Write(d.Hello("method"));?
????????????????????????Response.Write("<br />");?
????????????????????????d.Name =?"hello: property";?
????????????????????????Response.Write(d.Name);?
????????????????????????Response.Write("<br />");?
????????????????????????Response.Write(d["hello: indexer"]);?
????????????????????????Response.Write("<br />");?
????????????????????????Response.Write(d.GetNames().Count.ToString());?
????????????????????????// 注意:下面這句會報錯,因為不支持擴展方法?
????????????????????????// Response.Write(d.GetNames().Last());?
????????????????}?
????????}?
}?
/*?
運行結果:?
hello: method?
hello: property?
hello: indexer?
3?
*/?
3、泛型的協變的 Demo
Covariance.aspx.cs /*?
泛型協變規則:?
泛型參數受 out 關鍵字約束,隱式轉換目標的泛型參數類型必須是當前類型的“基類”?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?partial?class?Covariance : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????List<Human> human =?new?List<Human>();?
????????????????????????human.Add(new?Human { Name =?"aaa"?});?
????????????????????????human.Add(new?Human { Name =?"bbb"?});?
????????????????????????human.Add(new?Human { Name =?"ccc"?});?
????????????????????????List<Hero> hero =?new?List<Hero>();?
????????????????????????hero.Add(new?Hero { Name =?"ddd", Story =?"尿床"?});?
????????????????????????hero.Add(new?Hero { Name =?"eee", Story =?"撒謊"?});?
????????????????????????hero.Add(new?Hero { Name =?"fff", Story =?"打架"?});?
????????????????????????/*????
???????????????????????? * List<T> 實現了如下接口 IEnumerable<out T> ,所以可以實現協變?
???????????????????????? *?public?interface IEnumerable<out T> : IEnumerable?
???????????????????????? * {?
???????????????????????? *???????? // Summary:?
???????????????????????? *???????? //???????? Returns an enumerator that iterates through the collection.?
???????????????????????? *???????? //?
???????????????????????? *???????? // Returns:?
???????????????????????? *???????? //???????? A System.Collections.Generic.IEnumerator<T> that can be used?to?iterate through?
???????????????????????? *???????? //???????? the collection.?
???????????????????????? *???????? IEnumerator<T> GetEnumerator();?
???????????????????????? * }?
???????????????????????? */?
????????????????????????// Hero 的基類是 Human,所以 Hero 可以協變到 Human,所以下面的表達式成立?
????????????????????????List<Human> list = human.Union(hero).ToList();?
????????????????????????foreach (Human h?in?list)?
????????????????????????{?
????????????????????????????????Response.Write(h.Name);?
????????????????????????????????Response.Write("<br />");?
????????????????????????}?
????????????????}?
????????????????class?Human?
????????????????{?
????????????????????????public?string?Name {?get;?set; }?
????????????????}?
????????????????class?Hero : Human?
????????????????{?
????????????????????????public?string?Story {?get;?set; }?
????????????????}?
????????}?
}?
/*?
運行結果:?
aaa?
bbb?
ccc?
ddd?
eee?
fff?
*/?
4、泛型的逆變的 Demo
Contravariance.aspx.cs /*?
泛型逆變規則:?
泛型參數受?in?關鍵字約束,隱式轉換目標的泛型參數類型必須是當前類型的“子類”?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?partial?class?Contravariance : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????IOutput<Human> human =?new?Output<Human>();?
????????????????????????// 因為 IOutput<in?T> ,并且 Human 的子類是 Hero ,所以 IOutput<Human> 可以逆變到 IOutput<Hero>?
????????????????????????IOutput<Hero> hero = human;?
????????????????????????hero.Write(new?Hero { Name =?"webabcd"?});?
????????????????}?
????????????????interface IOutput<in?T>?
????????????????{?
????????????????????????void Write(T o);?
????????????????}?
????????????????class?Output<T> : IOutput<T>?
????????????????????????where T : Human?
????????????????{?
void Write() void Write(T o)?
????????????????????????{?
????????????????????????????????HttpContext.Current.Response.Write(o.Name);?
????????????????????????}?
????????????????}?
????????????????class?Human?
????????????????{?
????????????????????????public?string?Name {?get;?set; }?
????????????????}?
????????????????class?Hero : Human?
????????????????{?
????????????????????????public?string?Story {?get;?set; }?
????????????????}?
????????}?
}?
/*?
運行結果:?
webabcd?
*/ 5、CountdownEvent 的 Demo
CountdownEventDemo.aspx.cs /*?
* CountdownEvent - 線程、任務同步類。線程或任務一直阻塞到 CountdownEvent 的計數為 0 為止?
* 1、當有新的需要同步的線程或任務產生時,就調用 AddCount 增加 CountdownEvent 的計數?
* 2、當有線程或任務到達同步點時,就調用 Signal 函數減小 CountdownEvent 的計數?
* 3、當 CountdownEvent 的計數為 0 時,就表示所有需要同步的任務已經完成。通過 Wait 來阻塞線程?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
using System.Threading;?
namespace CSharp?
{?
????????public?partial?class?CountdownEventDemo : System.Web.UI.Page?
????????{?
????????????????private?string?_result = "";?
static readonly object objLock =?new?object() static readonly object objLock =?new?object();?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????// CountdownEvent(int initialCount) - 實例化一個 CountdownEvent?
????????????????????????//???????? int initialCount - 初始計數?
????????????????????????using (var countdown =?new?CountdownEvent(1))?
????????????????????????{?
????????????????????????????????Thread t1 =?new?Thread(() => ThreadWork("aaa", TimeSpan.FromSeconds(1), countdown));?
????????????????????????????????// 增加 1 個計數?
????????????????????????????????countdown.AddCount();?
????????????????????????????????t1.Start();?
????????????????????????????????Thread t2 =?new?Thread(() => ThreadWork("bbb", TimeSpan.FromSeconds(2), countdown));?
????????????????????????????????countdown.AddCount();?
????????????????????????????????t2.Start();?
????????????????????????????????Thread t3 =?new?Thread(() => ThreadWork("ccc", TimeSpan.FromSeconds(3), countdown));?
????????????????????????????????countdown.AddCount();?
????????????????????????????????t3.Start();?
????????????????????????????????// 減少 1 個計數?
????????????????????????????????countdown.Signal();?
????????????????????????????????// 阻塞當前線程,直到 CountdownEvent 的計數為零?
????????????????????????????????countdown.Wait();?
????????????????????????}?
????????????????????????Response.Write(_result);?
????????????????}?
void ThreadWork() void ThreadWork(string?name, TimeSpan sleepTime, CountdownEvent countdown)?
????????????????{?
????????????????????????Thread.Sleep(sleepTime);?
????????????????????????_result +=?"hello: "?+ name +?" "?+ DateTime.Now.ToString("HH:mm:ss");?
????????????????????????_result +=?"<br />";?
????????????????????????// 減少 1 個計數?
????????????????????????countdown.Signal();?
????????????????}?
????????}?
}?
/*?
運行結果:?
hello: aaa 15:18:55?
hello: bbb 15:18:56?
hello: ccc 15:18:57?
*/ 6、Barrier 的 Demo
BarrierDemo.aspx.cs /*?
* Barrier - 線程、任務同步類。其用來同步一個線程組或任務組中所有的線程或任務,先到達的線程或任務在此阻塞?
* 1、實例化 Barrier 指定其需要阻塞的線程或任務數?
* 2、通過 SignalAndWait ,可以實現當指定的線程或任務數完成的時候取消阻塞?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
using System.Threading;?
namespace CSharp?
{?
????????public?partial?class?BarrierDemo : System.Web.UI.Page?
????????{?
????????????????private?Barrier _barrier;?
????????????????private?string?_result = "";?
static readonly object objLock =?new?object() static readonly object objLock =?new?object();?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????// Barrier(int participantCount) - 實例化一個 Barrier?
????????????????????????//???????? int participantCount - 需要阻塞的相關線程或任務數?
????????????????????????_barrier =?new?Barrier(2);?
????????????????????????Thread t1 =?new?Thread(() => ThreadWork("aaa", TimeSpan.FromSeconds(1)));?
????????????????????????t1.Start();?
????????????????????????Thread t2 =?new?Thread(() => ThreadWork("bbb", TimeSpan.FromSeconds(2)));?
????????????????????????t2.Start();?
????????????????????????Thread t3 =?new?Thread(() => ThreadWork("ccc", TimeSpan.FromSeconds(3)));?
????????????????????????t3.Start();?
????????????????????????Thread.Sleep(5 * 1000);?
????????????????????????Response.Write(_result);?
????????????????}?
void ThreadWork() void ThreadWork(string?name, TimeSpan sleepTime)?
????????????????{?
????????????????????????lock (objLock)?
????????????????????????{?
????????????????????????????????_result +=?"Barrier之前:"?+ name +?" "?+ DateTime.Now.ToString("HH:mm:ss");?
????????????????????????????????_result +=?"<br />";?
????????????????????????}?
????????????????????????Thread.Sleep(sleepTime);?
????????????????????????// 當指定數量的線程或任務完成后,同步這些線程或任務?
????????????????????????_barrier.SignalAndWait();?
????????????????????????lock (objLock)?
????????????????????????{?
????????????????????????????????_result +=?"Barrier之后:"?+ name +?" "?+ DateTime.Now.ToString("HH:mm:ss");?
????????????????????????????????_result +=?"<br />";?
????????????????????????}?
????????????????}?
????????}?
}?
/*?
運行結果:?
Barrier之前:aaa 17:38:01?
Barrier之前:ccc 17:38:01?
Barrier之前:bbb 17:38:01?
Barrier之后:bbb 17:38:03?
Barrier之后:aaa 17:38:03?
*/ OK?
[源碼下載]?
? ? ?本文轉自webabcd 51CTO博客,原文鏈接:http://blog.51cto.com/webabcd/341285,如需轉載請自行聯系原作者
[源碼下載]
精進不休 .NET 4.0 (4) - C# 4.0 新特性之命名參數和可選參數, 動態綁定(dynamic), 泛型協變和逆變, CountdownEvent, Barrier
作者:webabcd
介紹
C# 4.0 的新特性
- Named And Optional Arguments - 命名參數和可選參數?
- Dynamic Binding - 動態綁定(dynamic 用于動態編程,其依賴于Dynamic Language Runtime)?
- Covariance - 泛型的協變?
- Contravariance - 泛型的逆變?
- CountdownEvent - 線程、任務同步類。線程或任務一直阻塞到 CountdownEvent 的計數為 0 為止
- Barrier - 線程、任務同步類。其用來同步一個線程組或任務組中所有的線程或任務,先到達的線程或任務在此阻塞
示例
1、?命名參數和可選參數的 Demo
NamedAndOptionalArguments.aspx.cs /*?
* 命名參數和可選參數?
* 命名參數:調用方法時,可以不按位置傳遞參數,而是指定參數的命名來傳值?
* 可選參數:聲明方法中的參數時,可以為其設置默認值,那么在調用該方法時,這種可選參數是可以忽略的?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?partial?class?NamedAndOptionalArguments : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????Write("hello");?
????????????????????????Write("hello",?"webabcd");?
????????????????????????Write("hello", p3:?false, p2:?"webabcd");?
????????????????}?
void Write() void Write(string?p1,?string?p2 =?"p2", bool p3 =?true)?
????????????????{?
????????????????????????Response.Write(string.Format("p1:{0}; p2:{1}; p3:{2}", p1, p2, p3.ToString()));?
????????????????????????Response.Write("<br />");?
????????????????}?
????????}?
}?
/*?
運行結果:?
p1:hello; p2:p2; p3:True?
p1:hello; p2:webabcd; p3:True?
p1:hello; p2:webabcd; p3:False?
*/ 2、dynamic 的 Demo
DynamicBinding.aspx.cs /*?
* dynamic - 用于動態編程,其依賴于Dynamic Language Runtime(DLR)?
*????
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?class?DyanmicDemo?
????????{?
string?Hello()?string?Hello(string?name)?
????????????????{?
????????????????????????return?"hello: "?+ name;?
????????????????}?
????????????????public?string?Name {?get;?set; }?
????????????????public?string?this[string?value]?
????????????????{?
????????????????????????get?
????????????????????????{?
????????????????????????????????return value;?
????????????????????????}?
????????????????}?
dynamic GetNames() dynamic GetNames()?
????????????????{?
????????????????????????List<string> names =?new?List<string>() {?"web",?"webabc",?"webabcd"?};?
?????????????????????????
????????????????????????return names;?
????????????????}?
????????}?
????????public?partial?class?DynamicBinding : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????dynamic d =?new?DyanmicDemo();?
????????????????????????Response.Write(d.Hello("method"));?
????????????????????????Response.Write("<br />");?
????????????????????????d.Name =?"hello: property";?
????????????????????????Response.Write(d.Name);?
????????????????????????Response.Write("<br />");?
????????????????????????Response.Write(d["hello: indexer"]);?
????????????????????????Response.Write("<br />");?
????????????????????????Response.Write(d.GetNames().Count.ToString());?
????????????????????????// 注意:下面這句會報錯,因為不支持擴展方法?
????????????????????????// Response.Write(d.GetNames().Last());?
????????????????}?
????????}?
}?
/*?
運行結果:?
hello: method?
hello: property?
hello: indexer?
3?
*/?
3、泛型的協變的 Demo
Covariance.aspx.cs /*?
泛型協變規則:?
泛型參數受 out 關鍵字約束,隱式轉換目標的泛型參數類型必須是當前類型的“基類”?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?partial?class?Covariance : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????List<Human> human =?new?List<Human>();?
????????????????????????human.Add(new?Human { Name =?"aaa"?});?
????????????????????????human.Add(new?Human { Name =?"bbb"?});?
????????????????????????human.Add(new?Human { Name =?"ccc"?});?
????????????????????????List<Hero> hero =?new?List<Hero>();?
????????????????????????hero.Add(new?Hero { Name =?"ddd", Story =?"尿床"?});?
????????????????????????hero.Add(new?Hero { Name =?"eee", Story =?"撒謊"?});?
????????????????????????hero.Add(new?Hero { Name =?"fff", Story =?"打架"?});?
????????????????????????/*????
???????????????????????? * List<T> 實現了如下接口 IEnumerable<out T> ,所以可以實現協變?
???????????????????????? *?public?interface IEnumerable<out T> : IEnumerable?
???????????????????????? * {?
???????????????????????? *???????? // Summary:?
???????????????????????? *???????? //???????? Returns an enumerator that iterates through the collection.?
???????????????????????? *???????? //?
???????????????????????? *???????? // Returns:?
???????????????????????? *???????? //???????? A System.Collections.Generic.IEnumerator<T> that can be used?to?iterate through?
???????????????????????? *???????? //???????? the collection.?
???????????????????????? *???????? IEnumerator<T> GetEnumerator();?
???????????????????????? * }?
???????????????????????? */?
????????????????????????// Hero 的基類是 Human,所以 Hero 可以協變到 Human,所以下面的表達式成立?
????????????????????????List<Human> list = human.Union(hero).ToList();?
????????????????????????foreach (Human h?in?list)?
????????????????????????{?
????????????????????????????????Response.Write(h.Name);?
????????????????????????????????Response.Write("<br />");?
????????????????????????}?
????????????????}?
????????????????class?Human?
????????????????{?
????????????????????????public?string?Name {?get;?set; }?
????????????????}?
????????????????class?Hero : Human?
????????????????{?
????????????????????????public?string?Story {?get;?set; }?
????????????????}?
????????}?
}?
/*?
運行結果:?
aaa?
bbb?
ccc?
ddd?
eee?
fff?
*/?
4、泛型的逆變的 Demo
Contravariance.aspx.cs /*?
泛型逆變規則:?
泛型參數受?in?關鍵字約束,隱式轉換目標的泛型參數類型必須是當前類型的“子類”?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
namespace CSharp?
{?
????????public?partial?class?Contravariance : System.Web.UI.Page?
????????{?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????IOutput<Human> human =?new?Output<Human>();?
????????????????????????// 因為 IOutput<in?T> ,并且 Human 的子類是 Hero ,所以 IOutput<Human> 可以逆變到 IOutput<Hero>?
????????????????????????IOutput<Hero> hero = human;?
????????????????????????hero.Write(new?Hero { Name =?"webabcd"?});?
????????????????}?
????????????????interface IOutput<in?T>?
????????????????{?
????????????????????????void Write(T o);?
????????????????}?
????????????????class?Output<T> : IOutput<T>?
????????????????????????where T : Human?
????????????????{?
void Write() void Write(T o)?
????????????????????????{?
????????????????????????????????HttpContext.Current.Response.Write(o.Name);?
????????????????????????}?
????????????????}?
????????????????class?Human?
????????????????{?
????????????????????????public?string?Name {?get;?set; }?
????????????????}?
????????????????class?Hero : Human?
????????????????{?
????????????????????????public?string?Story {?get;?set; }?
????????????????}?
????????}?
}?
/*?
運行結果:?
webabcd?
*/ 5、CountdownEvent 的 Demo
CountdownEventDemo.aspx.cs /*?
* CountdownEvent - 線程、任務同步類。線程或任務一直阻塞到 CountdownEvent 的計數為 0 為止?
* 1、當有新的需要同步的線程或任務產生時,就調用 AddCount 增加 CountdownEvent 的計數?
* 2、當有線程或任務到達同步點時,就調用 Signal 函數減小 CountdownEvent 的計數?
* 3、當 CountdownEvent 的計數為 0 時,就表示所有需要同步的任務已經完成。通過 Wait 來阻塞線程?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
using System.Threading;?
namespace CSharp?
{?
????????public?partial?class?CountdownEventDemo : System.Web.UI.Page?
????????{?
????????????????private?string?_result = "";?
static readonly object objLock =?new?object() static readonly object objLock =?new?object();?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????// CountdownEvent(int initialCount) - 實例化一個 CountdownEvent?
????????????????????????//???????? int initialCount - 初始計數?
????????????????????????using (var countdown =?new?CountdownEvent(1))?
????????????????????????{?
????????????????????????????????Thread t1 =?new?Thread(() => ThreadWork("aaa", TimeSpan.FromSeconds(1), countdown));?
????????????????????????????????// 增加 1 個計數?
????????????????????????????????countdown.AddCount();?
????????????????????????????????t1.Start();?
????????????????????????????????Thread t2 =?new?Thread(() => ThreadWork("bbb", TimeSpan.FromSeconds(2), countdown));?
????????????????????????????????countdown.AddCount();?
????????????????????????????????t2.Start();?
????????????????????????????????Thread t3 =?new?Thread(() => ThreadWork("ccc", TimeSpan.FromSeconds(3), countdown));?
????????????????????????????????countdown.AddCount();?
????????????????????????????????t3.Start();?
????????????????????????????????// 減少 1 個計數?
????????????????????????????????countdown.Signal();?
????????????????????????????????// 阻塞當前線程,直到 CountdownEvent 的計數為零?
????????????????????????????????countdown.Wait();?
????????????????????????}?
????????????????????????Response.Write(_result);?
????????????????}?
void ThreadWork() void ThreadWork(string?name, TimeSpan sleepTime, CountdownEvent countdown)?
????????????????{?
????????????????????????Thread.Sleep(sleepTime);?
????????????????????????_result +=?"hello: "?+ name +?" "?+ DateTime.Now.ToString("HH:mm:ss");?
????????????????????????_result +=?"<br />";?
????????????????????????// 減少 1 個計數?
????????????????????????countdown.Signal();?
????????????????}?
????????}?
}?
/*?
運行結果:?
hello: aaa 15:18:55?
hello: bbb 15:18:56?
hello: ccc 15:18:57?
*/ 6、Barrier 的 Demo
BarrierDemo.aspx.cs /*?
* Barrier - 線程、任務同步類。其用來同步一個線程組或任務組中所有的線程或任務,先到達的線程或任務在此阻塞?
* 1、實例化 Barrier 指定其需要阻塞的線程或任務數?
* 2、通過 SignalAndWait ,可以實現當指定的線程或任務數完成的時候取消阻塞?
*/?
using System;?
using System.Collections.Generic;?
using System.Linq;?
using System.Web;?
using System.Web.UI;?
using System.Web.UI.WebControls;?
using System.Threading;?
namespace CSharp?
{?
????????public?partial?class?BarrierDemo : System.Web.UI.Page?
????????{?
????????????????private?Barrier _barrier;?
????????????????private?string?_result = "";?
static readonly object objLock =?new?object() static readonly object objLock =?new?object();?
void Page_Load() void Page_Load(object sender, EventArgs e)?
????????????????{?
????????????????????????// Barrier(int participantCount) - 實例化一個 Barrier?
????????????????????????//???????? int participantCount - 需要阻塞的相關線程或任務數?
????????????????????????_barrier =?new?Barrier(2);?
????????????????????????Thread t1 =?new?Thread(() => ThreadWork("aaa", TimeSpan.FromSeconds(1)));?
????????????????????????t1.Start();?
????????????????????????Thread t2 =?new?Thread(() => ThreadWork("bbb", TimeSpan.FromSeconds(2)));?
????????????????????????t2.Start();?
????????????????????????Thread t3 =?new?Thread(() => ThreadWork("ccc", TimeSpan.FromSeconds(3)));?
????????????????????????t3.Start();?
????????????????????????Thread.Sleep(5 * 1000);?
????????????????????????Response.Write(_result);?
????????????????}?
void ThreadWork() void ThreadWork(string?name, TimeSpan sleepTime)?
????????????????{?
????????????????????????lock (objLock)?
????????????????????????{?
????????????????????????????????_result +=?"Barrier之前:"?+ name +?" "?+ DateTime.Now.ToString("HH:mm:ss");?
????????????????????????????????_result +=?"<br />";?
????????????????????????}?
????????????????????????Thread.Sleep(sleepTime);?
????????????????????????// 當指定數量的線程或任務完成后,同步這些線程或任務?
????????????????????????_barrier.SignalAndWait();?
????????????????????????lock (objLock)?
????????????????????????{?
????????????????????????????????_result +=?"Barrier之后:"?+ name +?" "?+ DateTime.Now.ToString("HH:mm:ss");?
????????????????????????????????_result +=?"<br />";?
????????????????????????}?
????????????????}?
????????}?
}?
/*?
運行結果:?
Barrier之前:aaa 17:38:01?
Barrier之前:ccc 17:38:01?
Barrier之前:bbb 17:38:01?
Barrier之后:bbb 17:38:03?
Barrier之后:aaa 17:38:03?
*/ OK?
[源碼下載]?
? ? ?本文轉自webabcd 51CTO博客,原文鏈接:http://blog.51cto.com/webabcd/341285,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的精进不休 .NET 4.0 (4) - C# 4.0 新特性之命名参数和可选参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图片上传预览
- 下一篇: 组策略 从入门到精通 (一) 组策略的还