zoukankan      html  css  js  c++  java
  • 一个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); 
                            } 
                    } 
            } 

  • 相关阅读:
    【原创】QTP中手动添加对象
    【转载】【缺陷预防技术】流程技术预防
    【资料】HP Loadrunner 11下载地址
    使用命令行操作VSS
    sql server 按时间段查询记录的注意事项
    Asp.net应用程序文件名重名引起的bug
    使用SQL语句查询表中重复记录并删除
    backgroundpositionx的兼容性问题
    关于Asp.net Development Server
    如何查看正在使用某个端口的应该程序
  • 原文地址:https://www.cnblogs.com/xugang/p/963750.html
Copyright © 2011-2022 走看看