加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

一个C#的加锁解锁示例

发布时间:2020-12-16 01:20:04 所属栏目:百科 来源:网络整理
导读:大家可以仿照这个方法做: ? using???System;? using???System.Collections.Generic;? using???System.Text;? namespace???Generic_Reusable? {? ????????interface???ILockable? ????????{? ????????????????void???Lock();? ????????????????void???Unlock
大家可以仿照这个方法做:
?
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读