开发者社区> 技术小胖子> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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

简介:
+关注继续查看


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
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Net; 
InBlock.gifusing System.Windows; 
InBlock.gifusing System.Windows.Controls; 
InBlock.gifusing System.Windows.Documents; 
InBlock.gifusing System.Windows.Input; 
InBlock.gifusing System.Windows.Media; 
InBlock.gifusing System.Windows.Media.Animation; 
InBlock.gifusing System.Windows.Shapes; 
InBlock.gif 
InBlock.gifnamespace Silverlight20.Thread 
InBlock.gif
InBlock.gif        public partial class EventWaitHandle : UserControl 
InBlock.gif        { 
InBlock.gif                // AutoResetEvent(bool state) - 通知其他线程是否可入的类,自动 Reset() 
InBlock.gif                //         bool state - 是否为终止状态,即是否禁止其他线程入内 
InBlock.gif                private System.Threading.AutoResetEvent autoResetEvent =    
InBlock.gif                        new System.Threading.AutoResetEvent(false); 
InBlock.gif 
InBlock.gif                // ManualResetEvent(bool state) - 通知其他线程是否可入的类,手动 Reset() 
InBlock.gif                //         bool state - 是否为终止状态,即是否禁止其他线程入内 
InBlock.gif                private System.Threading.ManualResetEvent manualResetEvent =    
InBlock.gif                        new System.Threading.ManualResetEvent(false); 
InBlock.gif 
InBlock.gif                private static int i; 
InBlock.gif 
InBlock.gif                public EventWaitHandle() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                        // 演示 AutoResetEvent 
InBlock.gif                        AutoResetEventDemo(); 
InBlock.gif 
InBlock.gif                        // 演示 ManualResetEvent 
InBlock.gif                        ManualResetEventDemo(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void AutoResetEventDemo() 
InBlock.gif                { 
InBlock.gif                        i = 0; 
InBlock.gif 
InBlock.gif                        for (int x = 0; x < 100; x++) 
InBlock.gif                        { 
InBlock.gif                                // 开 100 个线程去操作静态变量 i 
InBlock.gif                                System.Threading.Thread thread = 
InBlock.gif                                        new System.Threading.Thread(new System.Threading.ThreadStart(AutoResetEventDemoCallback)); 
InBlock.gif                                thread.Start(); 
InBlock.gif 
InBlock.gif                                // 阻塞当前线程,直到 AutoResetEvent 发出 Set() 信号 
InBlock.gif                                autoResetEvent.WaitOne(); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(1000); 
InBlock.gif                        // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果 
InBlock.gif                        txtAutoResetEvent.Text = i.ToString(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void AutoResetEventDemoCallback() 
InBlock.gif                { 
InBlock.gif                        try 
InBlock.gif                        { 
InBlock.gif                                int j = i + 1; 
InBlock.gif 
InBlock.gif                                // 模拟多线程并发操作静态变量 i 的情况 
InBlock.gif                                System.Threading.Thread.Sleep(5); 
InBlock.gif 
InBlock.gif                                i = j; 
InBlock.gif                        } 
InBlock.gif                        finally 
InBlock.gif                        { 
InBlock.gif                                // 发出 Set() 信号,以释放 AutoResetEvent 所阻塞的线程 
InBlock.gif                                autoResetEvent.Set(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif 
InBlock.gif                private void ManualResetEventDemo() 
InBlock.gif                { 
InBlock.gif                        i = 0; 
InBlock.gif 
InBlock.gif                        for (int x = 0; x < 100; x++) 
InBlock.gif                        { 
InBlock.gif                                // Reset() - 将 ManualResetEvent 变为非终止状态,即由此线程控制 ManualResetEvent, 
InBlock.gif                                //         其他线程排队,直到 ManualResetEvent 发出 Set() 信号(AutoResetEvent 在 Set() 时会自动 Reset()) 
InBlock.gif                                manualResetEvent.Reset(); 
InBlock.gif 
InBlock.gif                                // 开 100 个线程去操作静态变量 i 
InBlock.gif                                System.Threading.Thread thread = 
InBlock.gif                                        new System.Threading.Thread(new System.Threading.ThreadStart(ManualResetEventDemoCallback)); 
InBlock.gif                                thread.Start(); 
InBlock.gif 
InBlock.gif                                // 阻塞当前线程,直到 ManualResetEvent 发出 Set() 信号 
InBlock.gif                                manualResetEvent.WaitOne(); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(1000); 
InBlock.gif                        // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果 
InBlock.gif                        txtManualResetEvent.Text = i.ToString(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void ManualResetEventDemoCallback() 
InBlock.gif                { 
InBlock.gif                        try 
InBlock.gif                        { 
InBlock.gif                                int j = i + 1; 
InBlock.gif 
InBlock.gif                                // 模拟多线程并发操作静态变量 i 的情况 
InBlock.gif                                System.Threading.Thread.Sleep(5); 
InBlock.gif 
InBlock.gif                                i = j; 
InBlock.gif                        } 
InBlock.gif                        finally 
InBlock.gif                        { 
InBlock.gif                                // 发出 Set() 信号,以释放 ManualResetEvent 所阻塞的线程,同时 ManualResetEvent 变为终止状态) 
InBlock.gif                                manualResetEvent.Set(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
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
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Net; 
InBlock.gifusing System.Windows; 
InBlock.gifusing System.Windows.Controls; 
InBlock.gifusing System.Windows.Documents; 
InBlock.gifusing System.Windows.Input; 
InBlock.gifusing System.Windows.Media; 
InBlock.gifusing System.Windows.Media.Animation; 
InBlock.gifusing System.Windows.Shapes; 
InBlock.gif 
InBlock.gifnamespace Silverlight20.Thread 
InBlock.gif
InBlock.gif        public partial class Monitor : UserControl 
InBlock.gif        { 
InBlock.gif                private static readonly object objLock = new object(); 
InBlock.gif                private static int i; 
InBlock.gif                 
InBlock.gif                public Monitor() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                        i = 0; 
InBlock.gif 
InBlock.gif                        for (int x = 0; x < 100; x++) 
InBlock.gif                        { 
InBlock.gif                                // 开 100 个线程去操作静态变量 i 
InBlock.gif                                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork)); 
InBlock.gif                                thread.Start(); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(1000); 
InBlock.gif                        // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果 
InBlock.gif                        txtMsg.Text = i.ToString(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void DoWork() 
InBlock.gif                { 
InBlock.gif                        try 
InBlock.gif                        { 
InBlock.gif                                // Monitor - 提供同步访问对象的机制 
InBlock.gif 
InBlock.gif                                // Enter() - 在指定对象上获取排他锁 
InBlock.gif                                System.Threading.Monitor.Enter(objLock); 
InBlock.gif 
InBlock.gif                                int j = i + 1; 
InBlock.gif 
InBlock.gif                                // 模拟多线程并发操作静态变量 i 的情况 
InBlock.gif                                System.Threading.Thread.Sleep(5); 
InBlock.gif 
InBlock.gif                                i = j; 
InBlock.gif 
InBlock.gif                                // Exit() - 释放指定对象上的排他锁 
InBlock.gif                                System.Threading.Monitor.Exit(objLock); 
InBlock.gif                        } 
InBlock.gif                        finally 
InBlock.gif                        { 
InBlock.gif                                // code 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 

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

        </StackPanel> 
</UserControl>
 
ThreadStaticAttribute.xaml.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Net; 
InBlock.gifusing System.Windows; 
InBlock.gifusing System.Windows.Controls; 
InBlock.gifusing System.Windows.Documents; 
InBlock.gifusing System.Windows.Input; 
InBlock.gifusing System.Windows.Media; 
InBlock.gifusing System.Windows.Media.Animation; 
InBlock.gifusing System.Windows.Shapes; 
InBlock.gif 
InBlock.gifnamespace Silverlight20.Thread 
InBlock.gif
InBlock.gif        public partial class ThreadStaticAttribute : UserControl 
InBlock.gif        { 
InBlock.gif                // ThreadStatic - 所指定的静态变量对每个线程都是唯一的 
InBlock.gif                [System.ThreadStatic] 
InBlock.gif                private static int value; 
InBlock.gif 
InBlock.gif                // 一般的静态变量,对每个线程都是共用的 
InBlock.gif                private static int value2; 
InBlock.gif 
InBlock.gif                public ThreadStaticAttribute() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                        Demo(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void Demo() 
InBlock.gif                { 
InBlock.gif                        System.Threading.Thread thread = new System.Threading.Thread(DoWork); 
InBlock.gif                        thread.Name = "线程1"
InBlock.gif                        thread.Start(); 
InBlock.gif 
InBlock.gif                        System.Threading.Thread.Sleep(100); 
InBlock.gif 
InBlock.gif                        System.Threading.Thread thread2 = new System.Threading.Thread(DoWork2); 
InBlock.gif                        thread2.Name = "线程2"
InBlock.gif                        thread2.Start(); 
InBlock.gif 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void DoWork() 
InBlock.gif                { 
InBlock.gif                        for (int i = 0; i < 10; i++) 
InBlock.gif                        { 
InBlock.gif                                // 线程1对静态变量的操作 
InBlock.gif                                value++; 
InBlock.gif                                value2++; 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        string s = value.ToString(); // value - 本线程独有的静态变量 
InBlock.gif                        string s2 = value2.ToString(); // value2 - 所有线程共用的静态变量 
InBlock.gif 
InBlock.gif                        this.Dispatcher.BeginInvoke(delegate { txtMsg.Text = s + " - " + s2; }); 
InBlock.gif                        // this.Dispatcher.BeginInvoke(delegate { txtMsg.Text = value + " - " + value2; }); // 在UI线程上调用,所以value值为UI线程上的value值,即 0    
InBlock.gif                } 
InBlock.gif 
InBlock.gif                void DoWork2() 
InBlock.gif                { 
InBlock.gif                        for (int i = 0; i < 10; i++) 
InBlock.gif                        { 
InBlock.gif                                // 线程2对静态变量的操作 
InBlock.gif                                value++; 
InBlock.gif                                value2++; 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        string s = value.ToString(); // value - 本线程独有的静态变量 
InBlock.gif                        string s2 = value2.ToString(); // value2 - 所有线程共用的静态变量 
InBlock.gif 
InBlock.gif                        this.Dispatcher.BeginInvoke(delegate { txtMsg2.Text = s + " - " + s2; }); 
InBlock.gif                        // this.Dispatcher.BeginInvoke(delegate { txtMsg2.Text = value + " - " + value2; }); // 在UI线程上调用,所以value值为UI线程上的value值,即 0    
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 


     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/343911,如需转载请自行联系原作者

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Silverlight 16进制颜色转Color (含透明度处理)
转自http://www.cnblogs.com/NatureSex/archive/2012/02/17/2355929.html 由于Silverlight 不能引用System.Drawing 命名空间进行 颜色转换的一些处理,故写此函数来转换 public Color HtmlToC...
563 0
Silverlight 2 End to End教程:创建Digg搜索客户端
Part0 Silverlight 2 End to End系列教程:创建Digg搜索客户端         翻译人:ttzhang(Technology Life) 翻译时间:2008/10/24  22:02:59 作者:ScottGu 出处:ScottGu's Blog...
1162 0
Silverlight初级教程系列
目前网络上的silverlight的教程也有很多了。在这里我会以一个flasher的角度去讲解如何进行silverlight的开发。在初级的教程中每一个操作都会尽量用最简单的方法来实现。其目地是给那些一点都没接触过silverlight从0开始学的人看的。
809 0
Silverlight初级教程-动画
Silverlight初级教程 动画 在这一节里将要用到在“认识工作区”中提到的时间抽了。这里将和学习flash的过程一样做一个最简单的动画。 silverlight中的动画是以“storyboard”的形式表现出来。
720 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载