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

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

概述3、EventWaitHandle.xaml <UserControl x:Class="Silverlight20.Thread.EventWaitHandle"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microso 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()

                 //         bool state - 是否为终止状态,即是否禁止其他线程入内

                 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()

                {

                        i = 0;


                         for ( int x = 0; x < 100; x++)

                        {

                                 // reset() - 将 ManualresetEvent 变为非终止状态,即由此线程控制 ManualresetEvent,

                                 //         其他线程排队,直到 ManualresetEvent 发出 Set() 信号(autoresetEvent 在 Set() 时会自动 reset())

                                manualresetEvent.reset();


                                 // 开 100 个线程去 *** 作静态变量 i

                                System.Threading.Thread thread =

                                         new System.Threading.Thread( new System.Threading.ThreadStart(ManualresetEventDemoCallback));

                                thread.Start();


                                 // 阻塞当前线程,直到 ManualresetEvent 发出 Set() 信号

                                manualresetEvent.WaitOne();

                        }


                        System.Threading.Thread.Sleep(1000);

                         // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果

                        txtManualresetEvent.Text = i.ToString();

                }


                 private voID ManualresetEventDemoCallback()

                {

                         try

                        {

                                 int j = i + 1;


                                 // 模拟多线程并发 *** 作静态变量 i 的情况

                                System.Threading.Thread.Sleep(5);


                                i = j;

                        }

                         finally

                        {

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

                <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

{

         public partial class Monitor : UserControl

        {

                 private static Readonly object objLock = new object();

                 private static int i;

                

                 public Monitor()

                {

                        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(1000);

                         // 1 秒后 100 个线程都应该执行完毕了,取得 i 的结果

                        txtMsg.Text = i.ToString();

                }


                 private voID DoWork()

                {

                         try

                        {

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


                                 // Enter() - 在指定对象上获取排他锁

                                System.Threading.Monitor.Enter(objLock);


                                 int j = i + 1;


                                 // 模拟多线程并发 *** 作静态变量 i 的情况

                                System.Threading.Thread.Sleep(5);


                                i = j;


                                 // Exit() - 释放指定对象上的排他锁

                                System.Threading.Monitor.Exit(objLock);

                        }

                         finally

                        {

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

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

{

         public partial class ThreadStaticAttribute : UserControl

        {

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

                [System.ThreadStatic]

                 private static int value;


                 // 一般的静态变量,对每个线程都是共用的

                 private static int value2;


                 public ThreadStaticAttribute()

                {

                        InitializeComponent();


                        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()

                {

                         for ( int i = 0; i < 10; i++)

                        {

                                 // 线程2对静态变量的 *** 作

                                value++;

                                value2++;

                        }


                         string s = value.ToString(); // value - 本线程独有的静态变量

                         string s2 = value2.ToString(); // value2 - 所有线程共用的静态变量


                         this.dispatcher.BeginInvoke( delegate { txtMsg2.Text = s + " - " + s2; });

                         // this.dispatcher.BeginInvoke(delegate { txtMsg2.Text = value + " - " + value2; }); // 在UI线程上调用,所以value值为UI线程上的value值,即 0    

                }

        }

}     OK
[源码下载]
总结

以上是内存溢出为你收集整理的上接稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor全部内容,希望文章能够帮你解决上接稳扎稳打Silverlight(26) - 2.0线程之Lock, Interlocked, EventWaitHandle, Monitor所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1052282.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存