一个C#的加锁解锁示例
2007-11-19 09:59
537 查看
大家可以仿照这个方法做:
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);
}
}
}
}
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);
}
}
}
}
相关文章推荐
- Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
- C# 带参数线程的一个简单示例
- C#一个方法返回多个值示例
- 自己动手,写一个分布式系统(附c#代码示例)
- C#线程的一个示例,自己备忘(向多个控件传递值)
- 用C#编写一个进程外的COM组件示例代码讲解
- C# 下面的代码示例演示如何结束异步操作以获取请求的流,然后开始一个请求以获取响应。....
- 用VS2005写一个 C 的类库和用 C#来调用的示例
- 用C#编写一个进程外的COM组件示例代码讲解
- [COM Interop学习小结]实现一个C#调用C++的示例
- C#一个发牌机示例程序
- 一个C#委托的示例
- C#DataViewRowState一个数据表的数据状态示例
- C#编写的一个发送邮件的简单示例
- 利用C#迭代器的一个杨辉三角示例
- C# 实现一个可取消的多线程操作 示例
- C# 写的一个泛型类和泛型方法示例
- Asp.net 在网页编写C#代码示例-- 一个简单的web MsSql 命令执行环境
- 用VS2005写一个 C 的类库和用 C#来调用的示例
- c# .Net :Excel NPOI导入导出操作教程之数据库表信息数据导出到一个Excel文件并写到磁盘示例分享