C# Monitor 锁定对象和 计时重入 的例子

本文通过实例详细讲解了C#中Monitor类的使用,包括如何锁定对象以确保线程安全,以及如何实现计时重入锁的机制。通过对这些概念的理解和实践,读者将能够更好地掌握C#并发编程中的同步控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        #region One Monitor Object Test 

        readonly static object _Sync = new Object();
        const int _total = 100000;
        static long _Count = 0;
        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Task task = Task.Run(DoRrr);

            for (int i = 0; i < _total; i++)
            {
                bool lockToken = false;

                try
                {
                    Monitor.Enter(_Sync, ref lockToken);
                    _Count++;

                }
                finally
                {
                    if (lockToken)
                        Monitor.Exit(_Sync);
                }
            }

            task.Wait();




            xLabelResult.Content = _Count.ToString();
        }

        private void DoRrr()
        {
            for (int i = 0; i < _total; i++)
            {
                bool lockToken = false;

                try
                {
                    Monitor.Enter(_Sync, ref lockToken);
                    _Count--;

                }
                finally
                {
                    if (lockToken)
                        Monitor.Exit(_Sync);
                }
            }
        }





        #endregion



        #region Monitor Wait Test


        //先用这个来锁定目标
        private   void Btn_WaitTrigger_Click(object sender, RoutedEventArgs e)
        {
            var task = Task.Run(() =>
            {
                Monitor.Enter(_Sync);
                Thread.Sleep(10000);
                Monitor.Exit(_Sync);
            });
    }
        #endregion
    }


        // 再用这个尝试获取锁定的目标
        private   void Button_TryToGetIn_Click_1(object sender, RoutedEventArgs e)
        {
           var task =  Task.Run( () =>
            {

                if (Monitor.TryEnter(_Sync, 20000))
                {
                    MessageBox.Show("Get Trigger");
                }
                else
                {
                    MessageBox.Show("No not get inter");
                }
            });

           

 
        }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值