3、EventWaitHandle.xaml
<UserControl x:Class="Silverlight20.Thread.EventWaitHandle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left" Margin="5">
<TextBlock x:Name="txtAutoResetEvent" />
<TextBlock x:Name="txtManualResetEvent" />
</StackPanel>
</UserControl>
EventWaitHandle.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight20.Thread
{
public partial class EventWaitHandle : UserControl
{
// AutoResetEvent(bool state) - 通知其他線程是否可入的類,自動 Reset()
// bool state - 是否為終止狀态,即是否禁止其他線程入内
private System.Threading.AutoResetEvent autoResetEvent =
new System.Threading.AutoResetEvent(false);
// ManualResetEvent(bool state) - 通知其他線程是否可入的類,手動 Reset()
private System.Threading.ManualResetEvent manualResetEvent =
new System.Threading.ManualResetEvent(false);
private static int i;
public EventWaitHandle()
{
InitializeComponent();
// 示範 AutoResetEvent
AutoResetEventDemo();
// 示範 ManualResetEvent
ManualResetEventDemo();
}
private void AutoResetEventDemo()
i = 0;
for (int x = 0; x < 100; x++)
{
// 開 100 個線程去操作靜态變量 i
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(AutoResetEventDemoCallback));
thread.Start();
// 阻塞目前線程,直到 AutoResetEvent 發出 Set() 信号
autoResetEvent.WaitOne();
}
System.Threading.Thread.Sleep(1000);
// 1 秒後 100 個線程都應該執行完畢了,取得 i 的結果
txtAutoResetEvent.Text = i.ToString();
private void AutoResetEventDemoCallback()
try
int j = i + 1;
// 模拟多線程并發操作靜态變量 i 的情況
System.Threading.Thread.Sleep(5);
i = j;
finally
// 發出 Set() 信号,以釋放 AutoResetEvent 所阻塞的線程
autoResetEvent.Set();
private void ManualResetEventDemo()
// Reset() - 将 ManualResetEvent 變為非終止狀态,即由此線程控制 ManualResetEvent,
// 其他線程排隊,直到 ManualResetEvent 發出 Set() 信号(AutoResetEvent 在 Set() 時會自動 Reset())
manualResetEvent.Reset();
new System.Threading.Thread(new System.Threading.ThreadStart(ManualResetEventDemoCallback));
// 阻塞目前線程,直到 ManualResetEvent 發出 Set() 信号
manualResetEvent.WaitOne();
txtManualResetEvent.Text = i.ToString();
private void ManualResetEventDemoCallback()
// 發出 Set() 信号,以釋放 ManualResetEvent 所阻塞的線程,同時 ManualResetEvent 變為終止狀态)
manualResetEvent.Set();
}
}
4、Monitor.xaml
<UserControl x:Class="Silverlight20.Thread.Monitor"
<TextBlock x:Name="txtMsg" />
Monitor.xaml.cs
public partial class Monitor : UserControl
private static readonly object objLock = new object();
public Monitor()
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
txtMsg.Text = i.ToString();
private void DoWork()
// Monitor - 提供同步通路對象的機制
// Enter() - 在指定對象上擷取排他鎖
System.Threading.Monitor.Enter(objLock);
// Exit() - 釋放指定對象上的排他鎖
System.Threading.Monitor.Exit(objLock);
// code
5、ThreadStaticAttribute.xaml
<UserControl x:Class="Silverlight20.Thread.ThreadStaticAttribute"
<TextBlock x:Name="txtMsg2" />
ThreadStaticAttribute.xaml.cs
public partial class ThreadStaticAttribute : UserControl
// ThreadStatic - 所指定的靜态變量對每個線程都是唯一的
[System.ThreadStatic]
private static int value;
// 一般的靜态變量,對每個線程都是共用的
private static int value2;
public ThreadStaticAttribute()
Demo();
void Demo()
System.Threading.Thread thread = new System.Threading.Thread(DoWork);
thread.Name = "線程1";
thread.Start();
System.Threading.Thread.Sleep(100);
System.Threading.Thread thread2 = new System.Threading.Thread(DoWork2);
thread2.Name = "線程2";
thread2.Start();
void DoWork()
for (int i = 0; i < 10; i++)
// 線程1對靜态變量的操作
value++;
value2++;
string s = value.ToString(); // value - 本線程獨有的靜态變量
string s2 = value2.ToString(); // value2 - 所有線程共用的靜态變量
this.Dispatcher.BeginInvoke(delegate { txtMsg.Text = s + " - " + s2; });
// this.Dispatcher.BeginInvoke(delegate { txtMsg.Text = value + " - " + value2; }); // 在UI線程上調用,是以value值為UI線程上的value值,即 0
void DoWork2()
// 線程2對靜态變量的操作
this.Dispatcher.BeginInvoke(delegate { txtMsg2.Text = s + " - " + s2; });
// this.Dispatcher.BeginInvoke(delegate { txtMsg2.Text = value + " - " + value2; }); // 在UI線程上調用,是以value值為UI線程上的value值,即 0
OK
<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/343911,如需轉載請自行聯系原作者