如何杀死互斥对象? [英] How to kill a mutex object?

查看:76
本文介绍了如何杀死互斥对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于缓存更新,我想删除互斥锁(如果已存在)。



请告诉我在C#4.0中删除互斥锁的代码是什么



谢谢..

解决方案

Kill与delete不同,而且没有概念适用于 System.Threading.Mutex 。通常,CLI对象不需要删除,因为管理平台。但是使用 Mutex ,当不再需要此对象时,需要调用 IDisposable.Dispose(),并且此接口方法由 System.Threading.WaitHandle.Dispose()实现:

https://msdn.microsoft.com/en-us/library/dd384809%28v=vs.110%29.aspx [ ^ ]。



此外,实现此接口的唯一原因是那些 WaitHandle 类基于非托管Windows API对象。使用这个界面是非托管处理这些对象的常用技术。



另外,我不确定你真的需要 Mutex 。许多具有本机编程经验的用户都不知道.NET lock 机制: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx [ ^ ]。



-SA

For cache update, i want to delete the mutex if already exists.

Please let me know what is the code for deleting the mutex in C# 4.0

Thanks..

解决方案

"Kill" is not the same as "delete", and none of the concepts are applicable to System.Threading.Mutex. Generally, CLI objects don't need deletion, because the platform is managed. But with Mutex, you need to call IDisposable.Dispose() when this object is no longer needed, and this interface method is implemented by System.Threading.WaitHandle.Dispose():
https://msdn.microsoft.com/en-us/library/dd384809%28v=vs.110%29.aspx[^].

Moreover, the only reason why this interface is implemented is that those WaitHandle classes are based on unmanaged Windows API objects. Using this interface is the usual technique of unmanaged disposing of such objects.

Also, I'm not sure you really need Mutex. A number of users with native programming experience are unaware of .NET lock mechanism: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^].

—SA


这篇关于如何杀死互斥对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆