一个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); 
                        } 
                } 
        } 
时间:2019-10-11 12:13:26 阅读(5)

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...