稳扎稳打Silverlight(26)

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

介绍

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

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

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

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

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

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

示例

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
{
  public partial class Lock : UserControl
  {
    // 需要被 lock 的静态变量
    private static readonly object objLock = new object();
    private static int i;
    public Lock()
    {
      InitializeComponent();
      i = 0;
      for (int x = 0; x < 100; x++)
      {
        // 开 100 个线程去操作静态变量 i
        System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
        thread.Start();
      }
      System.Threading.Thread.Sleep(3000);
      // 3 秒后 100 个线程都应该执行完毕了,取得 i 的结果
      // 做了并发处理的结果为 100 ,去掉 lock 可得到不做并发处理的结果
      txtMsg.Text = i.ToString();
    }
    private void DoWork()
    {
      try
      {
        // lock() - 确保代码块完成运行,而不会被其他线程中断。其参数必须为一个引用类型的对象
        lock (objLock)
        {
          int j = i + 1;
          // 模拟多线程并发操作静态变量 i 的情况
          System.Threading.Thread.Sleep(10);
          i = j;
        }
      }
      finally
      {
        // code
      }
    }
  }
}

时间: 2024-09-07 16:03:01

稳扎稳打Silverlight(26)的相关文章

稳扎稳打Silverlight 2.0系列文章索引

在线DEMO http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html 1.稳扎稳打Silverlight(1) - 1.0实例之电子表 2.稳扎稳打Silverlight(2) - 1.0实例之支持录音和回放的钢琴(Silverlight+ASP.NET AJAX+DLINQ) 3.稳扎稳打Silverlight(3) - 2.0控件之Border, Button, Calendar, Canvas, CheckBox, Co

稳扎稳打Silverlight(4)

稳扎稳打Silverlight(4) - 2.0控件之DataGrid,DatePicker,Grid,GridSplitter,HyperlinkButton,Image 在线DEMO http://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html 示例 1.DataGrid.xaml <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly

稳扎稳打Silverlight(36)

返回"稳扎稳打Silverlight 3.0系列文章索引" 稳扎稳打Silverlight(36) - 3.0控件之TreeView,ListBox增强,DataGrid增强,MediaElement增强 介绍 Silverlight 3.0 控件一览: TreeView - 树控件 ListBox - 改进:支持多选 DataGrid - 改进:结合 PagedCollectionView 实现数据分组, 增加了一些编辑数据的相关事件, 结合 DataAnnotations 实现数据

稳扎稳打Silverlight(51)

稳扎稳打Silverlight(51) - 4.0绑定之数据验证IDataErrorInfo,INotifyDataErrorInfo 介绍 Silverlight 4.0 数据验证: * IDataErrorInfo - 对数据实体类提供自定义验证支持..NET Framework 也有此接口,可以方便移植 * INotifyDataErrorInfo - 对数据实体类提供自定义验证支持,比 IDataErrorInfo 功能更强大.INotifyDataErrorInfo 支持异步验证,这就

稳扎稳打Silverlight(47)

稳扎稳打Silverlight(47) - 4.0UI之操作剪切板,隐式样式,CompositeTransform,拖放外部文件到程序中 介绍 Silverlight 4.0 用户界面(UI)相关: * 操作剪切板 - 支持获取或设置剪切板中的文本信息 * 隐式样式(Implicit Style) - 将某种样式应用到某种类型的所有元素,即全局样式 * CompositeTransform - 将多种转换方式合而为一 * 拖动(Drag)外部文件,并将其放到(Drop) Silverlight

稳扎稳打Silverlight(46)

稳扎稳打Silverlight(46) - 4.0UI之FlowDirection,TextTrimming,响应鼠标滚轮事件,响应鼠标右键事件,全屏的新特性 介绍 Silverlight 4.0 用户界面(UI)相关: * FlowDirection - 指定文本或界面元素在它们的父元素中的流动方向 * TextTrimming - 文字溢出时的显示方式 * 响应鼠标的滚轮事件 * 响应鼠标的右键事件 * 全屏的新特性 - 当其他程序获得焦点时,是否退出全屏模式 在线DEMO http://w

稳扎稳打Silverlight(45)

稳扎稳打Silverlight(45) - 4.0浏览器外运行(Out Of Browser)之被信任的应用程序(Trusted Application) 介绍 Silverlight 4.0 OOB 之 被信任的应用程序: * 概述 * 访问本地文件系统 * 调用 COM 接口 * 自定义窗口样式和行为 在线DEMO http://www.cnblogs.com/webabcd/archive/2010/08/09/1795417.html 示例 1.关于"被信任的应用程序"的简要概

稳扎稳打Silverlight(44)

稳扎稳打Silverlight(44) - 4.0浏览器外运行(Out Of Browser)之OOB的增强及其新增的NotificationWindow 介绍 Silverlight 4.0 OOB 模式的新特性: * 新增了 Closing 事件 * 实现程序在 OOB 模式下的自动更新 * NotificationWindow - 在 OOB 模式下显示通知窗口,也就是 toast * 实现自定义的 NotificationWindow 在线DEMO http://www.cnblogs.

稳扎稳打Silverlight(41)

返回"稳扎稳打Silverlight 3.0系列文章索引" 稳扎稳打Silverlight(41) - 3.0Tip/Trick之GPU加速,Out-Of-Browser,应用程序库缓存,合并ResourceDictionary,应用程序扩展服务,Silverlight插件对象 介绍 Silverlight 3.0 提示和技巧系列 GPU 加速 - 对 GPU 加速的支持 Out-Of-Browser -浏览器外运行,即支持脱离浏览器运行 应用程序库缓存 - 将 dll(zip) 缓存