当前位置 : 主页 > 编程语言 > c语言 >

一个C#的加锁解锁示例

来源:互联网 收集:自由互联 发布时间:2021-06-25
大家可以仿照这个方法做: usingSystem; usingSystem.Collections.Generic; usingSystem.Text; namespaceGeneric_Reusable { interfaceILockable { voidLock(); voidUnlock(); } classSharedMemoryLock:ILockable { #regionILockableMembers publ
大家可以仿照这个方法做:
 
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); 
                        } 
                } 
        } 
时间:2019-10-11 12:13:26 阅读(5)
网友评论