• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
搬运工_阿壮
博客园    首页    新随笔    联系   管理    订阅  订阅

一个C#的加锁解锁示例

大家可以仿照这个方法做:
 
using   System; 
using   System.Collections.Generic; 
using   System.Text; 

namespace   Generic_Reusable 
{ 
        interface   ILockable 
        { 
                void   Lock(); 
                void   Unlock(); 
        } 

        class   SharedMemoryLock   :   ILockable 
        { 
                #region   ILockable   Members 

                public   void   Lock() 
                { 
                        Console.WriteLine( "SharedLock   performs   lock   method. "); 
                } 

                public   void   Unlock() 
                { 
                        Console.WriteLine( "SharedLock   performs   unlock   method. "); 
                } 

                #endregion 
        } 

        class   FileLock   :   ILockable 
        { 
                #region   ILockable   Members 

                public   void   Lock() 
                { 
                        Console.WriteLine( "FileLock   performs   lock   method. "); 
                } 

                public   void   Unlock() 
                { 
                        Console.WriteLine( "FileLock   performs   unlock   method. "); 
                } 

                #endregion 
        } 

        class   ReusableLock <LOCK >   :   ILockable   where   LOCK   :   ILockable,   new() 
        { 
                private   LOCK   lock__   =   new   LOCK(); 

                #region   ILockable   Members 

                public   void   Lock() 
                { 
                        lock__.Lock(); 
                } 

                public   void   Unlock() 
                { 
                        lock__.Unlock(); 
                } 

                #endregion 
        } 

        class   Program 
        { 
                static   void   Main(string[]   args) 
                { 
                        try 
                        { 
                                ReusableLock <SharedMemoryLock >   theLock   =   new   ReusableLock <SharedMemoryLock >(); 
                                theLock.Lock(); 
                                //   TODO:   Add   your   code   here 
                                theLock.Unlock(); 
                        } 
                        catch   (Exception   ex) 
                        { 
                                Console.WriteLine(ex.Message); 
                        } 
                } 
        } 
} 
作者: XuGang   网名:钢钢
出处: http://xugang.cnblogs.com
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址!
posted @ 2019-10-11 11:25  搬运工_阿壮  阅读(2289)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3