您的位置:首页 > 大数据 > 人工智能

稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor, ThreadStaticAttribute

2008-12-29 08:15 656 查看
[索引页]

[源码下载]

[align=center]稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor, ThreadStaticAttribute[/align]

作者:webabcd

介绍

Silverlight 2.0 使用Lock, Interlocked, EventWaitHandle, Monitor来实现线程同步

Lock - 确保代码块完成运行,而不会被其他线程中断

Interlocked - 为多个线程共享的变量提供原子级的操作

EventWaitHandle - 通知其他线程是否可入的类

Monitor - 提供同步访问对象的机制

ThreadStaticAttribute - 所指定的静态变量对每个线程都是唯一的

在线DEMO

/article/4589581.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">

<TextBlock x:Name="txtMsg" />

</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;

namespace Silverlight20.Thread

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">

<TextBlock x:Name="txtMsg" />

</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;

namespace Silverlight20.Thread

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

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">

<TextBlock x:Name="txtMsg" />

</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;

namespace Silverlight20.Thread

<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" />

</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;

namespace Silverlight20.Thread

OK

[源码下载]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐