[索引页]
[源码下载]
稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor, ThreadStaticAttribute
作者:
webabcd
介绍
Silverlight 2.0 使用Lock, Interlocked, EventWaitHandle, Monitor来实现线程同步
Lock - 确保代码块完成运行,而不会被其他线程中断
Interlocked - 为多个线程共享的变量提供原子级的操作
EventWaitHandle - 通知其他线程是否可入的类
Monitor - 提供同步访问对象的机制
ThreadStaticAttribute - 所指定的静态变量对每个线程都是唯一的
在线DEMO
http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html
示例
1、Lock.xaml
<
UserControl
x:Class
="Silverlight20.Thread.Lock"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
>
<
StackPanel
HorizontalAlignment
="Left"
Margin
="5"
>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
<
TextBlock
x:Name
="txtMsg"
/>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
</
StackPanel
>
</
UserControl
>
Lock.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;
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
namespace
Silverlight20.Thread
![](http://img.e-com-net.com/image/product/8bf85857a9e14d23a651e7c8ef856bcc.gif)
{
public partial class Lock : UserControl
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 需要被 lock 的静态变量
private static readonly object objLock = new object();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private static int i;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
public Lock()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
InitializeComponent();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
i = 0;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
for (int x = 0; x < 100; x++)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 开 100 个线程去操作静态变量 i
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
System.Threading.Thread.Sleep(3000);
// 3 秒后 100 个线程都应该执行完毕了,取得 i 的结果
// 做了并发处理的结果为 100 ,去掉 lock 可得到不做并发处理的结果
txtMsg.Text = i.ToString();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private void DoWork()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
try
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// lock() - 确保代码块完成运行,而不会被其他线程中断。其参数必须为一个引用类型的对象
lock (objLock)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
int j = i + 1;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 模拟多线程并发操作静态变量 i 的情况
System.Threading.Thread.Sleep(10);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
i = j;
}
}
finally
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// code
}
}
}
}
2、Interlocked.xaml
<
UserControl
x:Class
="Silverlight20.Thread.Interlocked"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
>
<
StackPanel
HorizontalAlignment
="Left"
Margin
="5"
>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
<
TextBlock
x:Name
="txtMsg"
/>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
</
StackPanel
>
</
UserControl
>
Interlocked.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;
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
namespace
Silverlight20.Thread
![](http://img.e-com-net.com/image/product/8bf85857a9e14d23a651e7c8ef856bcc.gif)
{
public partial class Interlocked : UserControl
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
private static int i;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
public Interlocked()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
InitializeComponent();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
i = 0;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
for (int x = 0; x < 100; x++)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 开 100 个线程去操作静态变量 i
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
System.Threading.Thread.Sleep(1000);
// 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果
txtMsg.Text = i.ToString();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private void DoWork()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
try
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// Interlocked - 为多个线程共享的变量提供原子级的操作(避免并发问题)
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// i 加 1
System.Threading.Interlocked.Increment(ref i);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// i 减 1
System.Threading.Interlocked.Decrement(ref i);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// i 加 1
System.Threading.Interlocked.Add(ref i, 1);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 如果 i 等于 100 ,则将 i 赋值为 101
System.Threading.Interlocked.CompareExchange(ref i, 101, 100);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 将 i 赋值为 1000
// System.Threading.Interlocked.Exchange(ref i, 1000);
}
finally
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// code
}
}
}
}
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"
>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
<
TextBlock
x:Name
="txtAutoResetEvent"
/>
<
TextBlock
x:Name
="txtManualResetEvent"
/>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
</
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;
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
namespace
Silverlight20.Thread
![](http://img.e-com-net.com/image/product/8bf85857a9e14d23a651e7c8ef856bcc.gif)
{
public partial class EventWaitHandle : UserControl
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// AutoResetEvent(bool state) - 通知其他线程是否可入的类,自动 Reset()
// bool state - 是否为终止状态,即是否禁止其他线程入内
private System.Threading.AutoResetEvent autoResetEvent =
new System.Threading.AutoResetEvent(false);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// ManualResetEvent(bool state) - 通知其他线程是否可入的类,手动 Reset()
// bool state - 是否为终止状态,即是否禁止其他线程入内
private System.Threading.ManualResetEvent manualResetEvent =
new System.Threading.ManualResetEvent(false);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private static int i;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
public EventWaitHandle()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
InitializeComponent();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 演示 AutoResetEvent
AutoResetEventDemo();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 演示 ManualResetEvent
ManualResetEventDemo();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private void AutoResetEventDemo()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
i = 0;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
for (int x = 0; x < 100; x++)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 开 100 个线程去操作静态变量 i
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(AutoResetEventDemoCallback));
thread.Start();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 阻塞当前线程,直到 AutoResetEvent 发出 Set() 信号
autoResetEvent.WaitOne();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
System.Threading.Thread.Sleep(1000);
// 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果
txtAutoResetEvent.Text = i.ToString();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private void AutoResetEventDemoCallback()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
try
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
int j = i + 1;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 模拟多线程并发操作静态变量 i 的情况
System.Threading.Thread.Sleep(5);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
i = j;
}
finally
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 发出 Set() 信号,以释放 AutoResetEvent 所阻塞的线程
autoResetEvent.Set();
}
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private void ManualResetEventDemo()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
i = 0;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
for (int x = 0; x < 100; x++)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// Reset() - 将 ManualResetEvent 变为非终止状态,即由此线程控制 ManualResetEvent,
// 其他线程排队,直到 ManualResetEvent 发出 Set() 信号(AutoResetEvent 在 Set() 时会自动 Reset())
manualResetEvent.Reset();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 开 100 个线程去操作静态变量 i
System.Threading.Thread thread =
new System.Threading.Thread(new System.Threading.ThreadStart(ManualResetEventDemoCallback));
thread.Start();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 阻塞当前线程,直到 ManualResetEvent 发出 Set() 信号
manualResetEvent.WaitOne();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
System.Threading.Thread.Sleep(1000);
// 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果
txtManualResetEvent.Text = i.ToString();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private void ManualResetEventDemoCallback()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
try
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
int j = i + 1;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 模拟多线程并发操作静态变量 i 的情况
System.Threading.Thread.Sleep(5);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
i = j;
}
finally
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 发出 Set() 信号,以释放 ManualResetEvent 所阻塞的线程,同时 ManualResetEvent 变为终止状态)
manualResetEvent.Set();
}
}
}
}
4、Monitor.xaml
<
UserControl
x:Class
="Silverlight20.Thread.Monitor"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
>
<
StackPanel
HorizontalAlignment
="Left"
Margin
="5"
>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
<
TextBlock
x:Name
="txtMsg"
/>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
</
StackPanel
>
</
UserControl
>
Monitor.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;
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
namespace
Silverlight20.Thread
![](http://img.e-com-net.com/image/product/8bf85857a9e14d23a651e7c8ef856bcc.gif)
{
public partial class Monitor : UserControl
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
private static readonly object objLock = new object();
private static int i;
public Monitor()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
InitializeComponent();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
i = 0;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
for (int x = 0; x < 100; x++)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 开 100 个线程去操作静态变量 i
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
thread.Start();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
System.Threading.Thread.Sleep(1000);
// 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果
txtMsg.Text = i.ToString();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
private void DoWork()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
try
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// Monitor - 提供同步访问对象的机制
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// Enter() - 在指定对象上获取排他锁
System.Threading.Monitor.Enter(objLock);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
int j = i + 1;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 模拟多线程并发操作静态变量 i 的情况
System.Threading.Thread.Sleep(5);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
i = j;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// Exit() - 释放指定对象上的排他锁
System.Threading.Monitor.Exit(objLock);
}
finally
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// code
}
}
}
}
5、ThreadStaticAttribute.xaml
<
UserControl
x:Class
="Silverlight20.Thread.ThreadStaticAttribute"
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
="txtMsg"
/>
<
TextBlock
x:Name
="txtMsg2"
/>
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
</
StackPanel
>
</
UserControl
>
ThreadStaticAttribute.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;
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
namespace
Silverlight20.Thread
![](http://img.e-com-net.com/image/product/8bf85857a9e14d23a651e7c8ef856bcc.gif)
{
public partial class ThreadStaticAttribute : UserControl
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// ThreadStatic - 所指定的静态变量对每个线程都是唯一的
[System.ThreadStatic]
private static int value;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
// 一般的静态变量,对每个线程都是共用的
private static int value2;
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
public ThreadStaticAttribute()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
InitializeComponent();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
Demo();
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
void Demo()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
System.Threading.Thread thread = new System.Threading.Thread(DoWork);
thread.Name = "线程1";
thread.Start();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
System.Threading.Thread.Sleep(100);
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
System.Threading.Thread thread2 = new System.Threading.Thread(DoWork2);
thread2.Name = "线程2";
thread2.Start();
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
void DoWork()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
for (int i = 0; i < 10; i++)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 线程1对静态变量的操作
value++;
value2++;
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
string s = value.ToString(); // value - 本线程独有的静态变量
string s2 = value2.ToString(); // value2 - 所有线程共用的静态变量
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
this.Dispatcher.BeginInvoke(delegate
{ txtMsg.Text = s + " - " + s2; });
// this.Dispatcher.BeginInvoke(delegate { txtMsg.Text = value + " - " + value2; }); // 在UI线程上调用,所以value值为UI线程上的value值,即 0
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
void DoWork2()
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
for (int i = 0; i < 10; i++)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
{
// 线程2对静态变量的操作
value++;
value2++;
}
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
string s = value.ToString(); // value - 本线程独有的静态变量
string s2 = value2.ToString(); // value2 - 所有线程共用的静态变量
![](http://img.e-com-net.com/image/product/b5ccfbfdc70d443ba19cbdc6a5005b86.gif)
![](http://img.e-com-net.com/image/product/437570a32a8e47669b1b8f34e2a85bac.gif)
this.Dispatcher.BeginInvoke(delegate
{ txtMsg2.Text = s + " - " + s2; });
// this.Dispatcher.BeginInvoke(delegate { txtMsg2.Text = value + " - " + value2; }); // 在UI线程上调用,所以value值为UI线程上的value值,即 0
}
}
}
OK
[源码下载]